Javascript 从json构造承诺流

Javascript 从json构造承诺流,javascript,json,promise,Javascript,Json,Promise,我的程序具有以下流程: Promise.resolve(phase_1) .then(phase_2) .then(phase_3) .then(phase_4) .catch(errorHandler); 但我只想要flow.json中的阶段: [ "phase_1", "phase_2", "phase_3", "phase_4", ] 但也取得了同样的效果。可能吗 我如何尝试: let returning; flow.forEach(function(p

我的程序具有以下流程:

Promise.resolve(phase_1)
  .then(phase_2)
  .then(phase_3)
  .then(phase_4)
  .catch(errorHandler);
但我只想要flow.json中的阶段:

[
  "phase_1",
  "phase_2",
  "phase_3",
  "phase_4",
]
但也取得了同样的效果。可能吗

我如何尝试:

let returning;
flow.forEach(function(phase, index) => {
   if (!index && !returning) {
     returning = Promise.resolve(require(phase));
   } else {
     returning.then(require(phase));
   }
});

谢谢

如果这些阶段相互关联,那么您可以使用Promises和Generator来利用ASIC工作流,例如;(可怜人的共同计划)

['1','2','3'].forEach(e=>{function*迭代(索引){
//这里有些逻辑
var val=yielpromise.resolve(index).then(v=>{console.log(v);//用v做些什么
it.next(v);//作业队列异步
});
}
var it=迭代(e);
it.next();//启动引擎

});您当前所做的与

let returning = Promise.resolve(phase);
returning.then(flow[0]);
returning.then(flow[1]);
returning.then(flow[2]);
returning.then(flow[3]);
要获得实际的链接,您需要在代码中执行
returning=returning。然后(…)
。但是,您可以(并且应该)通过使用而不是
forEach
(这也解决了不在循环之外进行初始化的问题)来简化它:

请注意,
phase
应该是这里的一个函数,所以如果
flow
真的由字符串组成,那么您可能必须这样做

    return chain.then(() => doSomething(phase));

如果这些阶段不相关,您可以执行
phasePromises=Promise.all(phases.map(phase=>Promise.resolve(phase));phasePromises.then(onFulfillment,onFailure)
    return chain.then(() => doSomething(phase));