Javascript 如何手动执行 Star Promise Chain

Javascript How to Star Promise Chain execution Manually

我有一个这样构建的承诺链:

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);
}

我想在它完全建立后手动开始执行(而不是通过调用 Promise.resolve() 立即执行)这样做的技巧是什么?

Promise 只是一个值的代理。你不 "run a promise" 就像你不 "run a number" 一样。你做的运行,是一个函数:

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);
    }
}

并从 doTheThing(); 开始。