I'm currently attempting to chain another .then() to the end of a recursive promise chain with the Bluebird
library.
My code looks something like this
exports.fetchAll = function() {
fetchItems = function(items) {
return somethingAsync()
.then(function(response) {
items.push(response.data);
if (response.paging.next) {
fetchItems();
} else {
return items;
}
})
.catch(function(error) {
console.log(error);
});
}
return fetchItems([], 0);
}
/// In Some other place
fetchAll().then(function(result){ console.log(result); });
As of now, .then
at the end of the fetchAll call is returned immediately. How do I make it such that it executes at the end of my recursive chain?
When you are recursively invoking the function, fetchItems
, you need to return the value, like this
if (response.paging.next) {
return fetchItems(); // Note the `return` statement
} else {
return items;
}
Now, the fetchItems
returns another promise and the then
at the end will be invoked only after that promise is resolved.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.