简体   繁体   English

循环链顺序的Javascript承诺失败

[英]Javascript promise for loop chain order fail

I try test javascript promise loop chain, but I fail, the promise loop not order I wish, my sample like this(1 set timeout, 3 promise function): 我尝试测试javascript promise循环链,但失败了,promise循环不符合我的要求,我的示例如下(1个设置超时,3个promise函数):

//timeout
timeout = function (fn, pkg) {
    var t = setTimeout(function () {
        fn(pkg);
        clearTimeout(t);
    }, 1000);
};
//Promise function
p01 = function (check) {
    return new Promise(function (RS, RJ) {
        var echo = 'Run P01';
        timeout(RS, echo);
    });
};
p02 = function (check) {
    return new Promise(function (RS, RJ) {
        var echo = 'Run P02';
        timeout(RS, echo);
    });
};
p03 = function (check) {
    return new Promise(function (RS, RJ) {
        var echo = 'Run P03';
        timeout(RS, echo);
    });
};

//do for loop test
for (var i = 0; i < 3; i++) {
    p01().then(function (echo) {
        console.log(echo);
        return p02();
    }).then(function (echo) {
        console.log(echo);
        return p03();
    }).then(function (echo) {
        console.log(echo);
        console.log('Done!');
    });
};

when loop done, log show me that: 当循环完成时,日志显示给我:

(3)Run P01
(3)Run P02
Run P03
Done!
Run P03
Done!
Run P03
Done!

I don't know what wrong happen, how do I fix that to get like this: 我不知道发生了什么错误,如何解决这个问题:

Run P01
Run P02
Run P03
Done!
Run P01
Run P02
Run P03
Done!
Run P01
Run P02
Run P03
Done!

thank you for help! 谢谢你的帮助!

Currently, each iteration in your for loop is running synchronously , one after another. 当前, for循环中的每个迭代都一次又一次地同步运行。 The Promise chain gets initialized, but you aren't waiting for it to resolve before going on to the next iteration (and initializing another Promise chain). Promise链已初始化,但是在进行下一个迭代(并初始化另一个Promise链)之前,您无需等待它解决。

Either put the loop in an async function and await the Promise chain on each iteration (so that the next iteration doesn't start until the current iteration's has completely finished): 要么将循环放入async函数中,然后在每次迭代中await Promise链(这样,直到当前迭代完全完成后,下一个迭代才会开始):

 //timeout timeout = function(fn, pkg) { var t = setTimeout(function() { fn(pkg); clearTimeout(t); }, 1000); }; //Promise function p01 = function(check) { return new Promise(function(RS, RJ) { var echo = 'Run P01'; timeout(RS, echo); }); }; p02 = function(check) { return new Promise(function(RS, RJ) { var echo = 'Run P02'; timeout(RS, echo); }); }; p03 = function(check) { return new Promise(function(RS, RJ) { var echo = 'Run P03'; timeout(RS, echo); }); }; //do for loop test (async () => { for (var i = 0; i < 3; i++) { await p01().then(function(echo) { console.log(echo); return p02(); }).then(function(echo) { console.log(echo); return p03(); }).then(function(echo) { console.log(echo); console.log('Done!'); }); } })(); 

Or have an outer Promise that you reassign and call .then on each time: 或有一个外部承诺,您每次都重新分配并致电.then

 //timeout timeout = function (fn, pkg) { var t = setTimeout(function () { fn(pkg); clearTimeout(t); }, 1000); }; //Promise function p01 = function (check) { return new Promise(function (RS, RJ) { var echo = 'Run P01'; timeout(RS, echo); }); }; p02 = function (check) { return new Promise(function (RS, RJ) { var echo = 'Run P02'; timeout(RS, echo); }); }; p03 = function (check) { return new Promise(function (RS, RJ) { var echo = 'Run P03'; timeout(RS, echo); }); }; //do for loop test let prom = Promise.resolve(); for (var i = 0; i < 3; i++) { prom = prom.then(p01).then(function (echo) { console.log(echo); return p02(); }).then(function (echo) { console.log(echo); return p03(); }).then(function (echo) { console.log(echo); console.log('Done!'); }); }; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM