Reputation: 284
I have a bit of a messy problem I can't find a proper answer for. It's in firebase which doesn't support await and async.
At the moment, it works, but only runs once and doesn't loop.
return foo.bar(query);})
.then((result) => {
if (result == '0') {
//do stuff
}
else {
var i;
for(i = 0; i <= result; i++) {
return foo.bar(secondQuery);})
.then((secondResult) => {
//do other stuff
})
}
}
})
Upvotes: 3
Views: 3335
Reputation: 3537
You can use Promise.all
The Promise.all(iterable) method returns a single Promise that resolves when all of the promises in the iterable argument have resolved or when the iterable argument contains no promises. It rejects with the reason of the first promise that rejects.
return foo.bar(query).then(result => {
if (result == '0') {
//do stuff
} else {
var i;
var all = [];
for (i = 0; i <= result; i++) {
var p = foo.bar(secondQuery).then((secondResult) => {
//do other stuff
});
all.push(p)
}
return Promise.all(all);
}
});
Upvotes: 3