简体   繁体   中英

Javascript How to Star Promise Chain execution Manually

I have a chain of promises built like this:

var chain = Promise.resolve(); // execution starts immediately

for (var i = 0; i < 100; i++) {
   var promise = new Promise(...) // build another one
   chain = chain.then(promise);
}

I want to start its execution manually after it's been fully built up (as opposed to immediately as it happens by calling Promise.resolve() ) what's the technique for doing that?

A Promise is merely a proxy for a value. You don't "run a promise" like you don't "run a number". What you do run, is a function:

function doTheThing() {
    var chain = Promise.resolve(); // execution starts immediately

    for (var i = 0; i < 100; i++) {
       var promise = new Promise(...) // build another one
       chain = chain.then(promise);
    }
}

And start with doTheThing(); .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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