0
async lsEntered(){ if(this.service.wd == '') { await basic((this.service.wd)); } else { await basic(('/'+this.service.wd)); } this.files = await JSON.parse(localStorage.getItem('FILENAMES')); var filesList = document.getElementById(this.trackLine.toString()); var li; for (var i = 0; i < this.files.length; i++) { li = document.createElement('li'); li.appendChild(document.createTextNode(this.files[i].name)); filesList.appendChild(li); } localStorage.clear(); } 

I want to wait until basic is finished and JSON.parse finishes before displaying the values in the DOM. I'm getting the values of the previous call every time which is tell me the async is no working. To be fair I don't have tons of TS experience.

Edit: This is basic I was hoping not to have to deal with it as it's a javascript function and fragily integrated into this app.

var basic = function (path) { var ACCESS_TOKEN = ''; var dbx = new Dropbox({ accessToken: ACCESS_TOKEN }); dbx.filesListFolder({ path: path }) .then(function (response) { localStorage.setItem('FILENAMES',JSON.stringify(response.entries)); console.log(response); }) .catch(function (error) { console.error(error); }); return false; } 
8
  • Maybe there is an error in basic. Commented Aug 24, 2017 at 2:09
  • 3
    You are not returning a promise in basic Commented Aug 24, 2017 at 2:13
  • 2
    You might want to invalidate that access token, now that everybody can see it on the edit history Commented Aug 24, 2017 at 2:15
  • 2
    JSON.parse does not return a promise, so you should omit the await there Commented Aug 24, 2017 at 2:16
  • 3
    Instead of storing the result in the localstorage, you should resolve the promise with it. That would make basic much more flexible to use. Commented Aug 24, 2017 at 2:18

2 Answers 2

3
let myPromise = new Promise((resolve, reject) => { // Work you want to execute resolve("I am done"); }); myPromise.then((successMessage) => { // successMessage is whatever we passed in the resolve(...) function above. console.log("Yay! " + successMessage); }); 
Sign up to request clarification or add additional context in comments.

3 Comments

Where and how do I use promise? I'm new to this game.
Pretty helpful but I think I need to divide the code into three parts. How can I add another myPromise.then it's shooting compile errors when I try to add a third.
Try create an array of promises you use promise.all(). You'll find in this link developer.mozilla.org/en/docs/Web/JavaScript/Reference/…
3

You can only await promises1 and basic is not returning a promise. You need to do

return dbx.filesListfolder(...)... 

Also consider what Bergi said in their comment.


1: Actually, you can await any value, but there is no point in awaiting something that is not a promise. By not returning the promise from basic, lsEntered won't wait for the local storage to be set.

2 Comments

Do I need to declare this return of dbx.filesListFolder as a promise somewhere?
I spent 3 hours trying to work with the typings and decided on doing this instead :(

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.