【发布时间】:2021-03-02 00:05:52
【问题描述】:
这是我第一次用 Promise 和回调编写 while 循环。我不知道为什么它会导致无限循环。如何解决?
async function getResult(){
return new Promise((resolve, reject) => {
let params ="some input and setting";
let next = "hasNext";
let array = [];
let error = null;
while(next !== null){
checkNext(params, function(err,data) { //checkNext is a function to return the current list and check wether there has the next list
if(err){
next = null;
error = err;
}else{
next = data.hasNext; // if there is not next list, data.hasNext = null
array = array.concat(data.array); // data.array return the current list
}
});
}
if(error !== null){
reject(error);
}else{
resolve(array); // I want to return all lists
}
});
}
【问题讨论】:
标签: node.js loops promise callback