admin管理员组文章数量:1332889
I am trying to call an asynchronous function recursively using javascript promises but haven't found a pattern that works.
This is what I imagine would work:
var doAsyncThing = function(lastId){
new Promise(function(resolve, reject){
// async request with lastId
return resolve(response)
}
}
var recursivelyDoAsyncThing = function(lastId){
doAsyncThing(lastId).then(function(response){
return new Promise(function(resolve, reject){
//do something with response
if(response.hasMore){
//get newlastId
return resolve(recursivelyDoAsyncThing(newLastId));
}else{
resolve();
}
});
});
}
recursivelyDoAsyncThing().then( function(){
console.log('done');
});
Why doesn't this work? What have I misunderstood?
Is there a better pattern to solve this problem?
I am trying to call an asynchronous function recursively using javascript promises but haven't found a pattern that works.
This is what I imagine would work:
var doAsyncThing = function(lastId){
new Promise(function(resolve, reject){
// async request with lastId
return resolve(response)
}
}
var recursivelyDoAsyncThing = function(lastId){
doAsyncThing(lastId).then(function(response){
return new Promise(function(resolve, reject){
//do something with response
if(response.hasMore){
//get newlastId
return resolve(recursivelyDoAsyncThing(newLastId));
}else{
resolve();
}
});
});
}
recursivelyDoAsyncThing().then( function(){
console.log('done');
});
Why doesn't this work? What have I misunderstood?
Is there a better pattern to solve this problem?
Share Improve this question asked Feb 15, 2016 at 18:46 benjaminjosephwbenjaminjosephw 4,4173 gold badges22 silver badges41 bronze badges3 Answers
Reset to default 3recursivelyDoAsyncThing
needs to return a Promise in order to continue the chain. In your case, all you need to do is have doAsyncThing
return its Promise:
var doAsyncThing = function(lastId){
// Notice the return here:
return new Promise(function(resolve, reject){
Then add return
to your doAsyncThing
call like so:
var recursivelyDoAsyncThing = function(lastId){
// Notice the return here:
return doAsyncThing(lastId).then(function(response){
You're missing a return
in the recursivelyDoAsyncThing
function. Also you should avoid the Promise
constructor antipattern:
function recursivelyDoAsyncThing(lastId) {
return doAsyncThing(lastId).then(function(response) {
//^^^^^^
//do something with response
if (response.hasMore) {
//get newlastId
return recursivelyDoAsyncThing(newLastId);
} else {
return; // undefined? Always return a useful value
}
});
}
I have a simple example of recursive promise. The example is based on calculation factorial
of number.
let code = (function(){
let getFactorial = n =>{
return new Promise((resolve,reject)=>{
if(n<=1){
resolve(1);
}
resolve(
getFactorial(n-1).then(fact => {
return fact * n;
})
)
});
}
return {
factorial: function(number){
getFactorial(number).then(
response => console.log(response)
)
}
}
})();
code.factorial(5);
code.factorial(6);
code.factorial(7);
本文标签: javascriptHow to call promise function recursivelyStack Overflow
版权声明:本文标题:javascript - How to call promise function recursively - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742307751a2450258.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论