我有一个异步函数,在其中调用另一个函数返回一个promise,同时还调用另一个异步函数。这是代码:
async function asyncAwaitRepeat(index) {
if(index < 9) {
await promise_animate(rectPointer); // the promise_animate return a promise
await asyncAwaitRepeat(index + 1); // but my promise_animate never gets resolved
}
}
asyncAwaitRepeat(asyncAwaitRepeatindex);
因为我有一些异步代码要执行,所以我不能直接调用resolve()。所以我用回电
function promise_animate (index) {
return new Promise((resolve) => {
animate(index, () => {resolve}); // since animate function is a asynchronous so we
//cannot directly resolve() it
})
}
animate函数本身是异步的,做了很多异步的事情
async function animate(rectIndex, animationPromiseFulfilled) {
if(rectIndex < 8) {
await promise_hightlight(rectIndex, "red");
if( rectArray[rectIndex].value > rectArray[rectIndex+1].value ) {
await promise_hightlight(rectIndex, "red");
// doing a bunch of asynchronous stuff
await animate(rectIndex+1, animationPromiseFulfilled); // << Here i call the
// function recursively
}
else if(rectIndex == 8) {
await promise_hightlight(rectIndex, "red");
if( rectArray[rectIndex].value > rectArray[rectIndex+1].value ) {
await promise_hightlight(rectIndex, "red");
// some more asynchronous stuff
}
await promise_hightlight(rectIndex, "green");
rectPointer = 0;
animationPromiseFulfilled("the animation function resolved");//<< the promise
//resolve done
}
}
但是fullfiled()没有完成其工作,也没有解决。请帮助
我认为您的决心永远不会得到呼唤,因此您必须尝试这样:
function promise_animate (index) {
return new Promise(async(resolve) => {
resolve(await animate(index));
})
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句