0

EDIT: changed the code as suggested by the answer, using promises. It prints before Added all tags and then the various Found

I have the following code to fill an array with some keys from Firebase:

var userTags = [];
var arrayPromises = [];
user_pref.once('value', function(preferenze){ 
preferenze.forEach(function(t){
  ref.once('value', function(tags){ 
  arrayPromises.push(arrayPromises.push(new Promise(function (resolve, reject) {
  tags.forEach(function (t1){
    if(t.key == t1.key){ 
    console.log("Found " + t1.key)

    }
    return false;

  })}));
})
return false;
})
})
Promise.all(arrayPromises).then(()=>{
console.log("Added all tags")

})
}

I've added the then clause to be sure I call the method findPoiByTagis called after I filled the array, but apparently the code inside then is executed before the rest. In fact, the message lenght 2: is printed before the other message Lenght, with the consequence that it first prints "0" and then it prints the correct lenght while filling the array.

EDIT: it prints the lenght of the array a number of time equal to the number of forEach iterations, so the return false statement does not break the loop.

I've always used this method to wait for the instructions to be finished, why now is not working? Did I miss something?

1 Answer 1

1

oh well i see now, the then function is after the push function not afther the forEach so you will have to do something like this

var arrayPromises = [] 
forEach( 
push( 
... 
generatePromise 
... 
).then( 
... 
promiseRecieved 
checkForAllPromisesRecieved(doTheThing)
... ) ) 

function doTheThing(){
   this.myTags = userTags; 
   console.log("Lenght 2: " + userTags.length) 
   this.findPoiByTag(this.myTags);
}
Sign up to request clarification or add additional context in comments.

7 Comments

if I delete the "return false" it gives me an error, saying that I have to return a boolean.
I've used this code before, same as this, Firebase usage needs this construct. It doesn't normally stop when there a return false, since it requires it. Also, if it would really return after first iteration, then it would print just once the lenght of the array, instead it prints it for every iteration.
oh well i see now, the then function is after the push function not afther the forEach so you will have to do something like this var arrayPromises = [] forEach( push( ... generatePromise ... ).then( ... promiseRecieved ... ) ) //after all promises this.myTags = userTags; console.log("Lenght 2: " + userTags.length) this.findPoiByTag(this.myTags);
i changed my anwer :)
Thanks! I'm not sure how to use promises, I've changed my code in the question to show you how I've used them since it doesn't work fine the way I did :\
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.