Execute promises map sequentially

前端 未结 3 1023
梦谈多话
梦谈多话 2021-01-18 22:12

I have written a function that is being called in a loop (map) and that function is using promises. Now, I want that function to run synchronously and exit before its next i

3条回答
  •  被撕碎了的回忆
    2021-01-18 22:31

    (function loop(index) {
        const next = promiseArray[index];
        if (!next) {
            return;
        }
        next.then((response) => {
            // do Something before next
            loop(index + 1);
        }).catch(e => {
            console.error(e);
            loop(index + 1);
        });
    })(0 /* startIndex */)
    

提交回复
热议问题