[英]javascript ES6 dynamically chaining promises
我正在尝试动态地链接promise,以处理未知数量的需要按顺序进行的异步调用。 我正在使用IO.JS / chrome,它本身支持Promise。
立即创建诺言(至少相对于控制台输出而言)。 我原本希望能够收集承诺,然后将其传递给Promise.all,但是到那时,由于我不了解的原因,它们已经被解雇了。
这是链接的一种方法,这是对Javascript Promises中的动态链接的评论所建议的
var lastPr = null;
console.log(" exit setup......................");
while(this.statesToExit.length > 0) {
var v = this.statesToExit.shift();
console.log("new Promise...");
var pr = new Promise(function (resolve, reject) {
console.log("doing Exit stuff at time " +curTime);
resolve(); //SOMETHING MORE SUBSTANTIAL GOES HERE
});
console.log("lastPr.then.");
if (lastPr != null) {
lastPr.then(pr);
}
lastPr = pr;
// console.log("adding pr to worklist");
// promiseList.push(pr);
// });
}
另一种方法是
var promiseList= [];
console.log(" exit setup......................");
while(this.statesToExit.length > 0) {
var v = this.statesToExit.shift();
console.log("new Promise...");
var pr = new Promise(function (resolve, reject) {
console.log("doing Exit stuff at time " +curTime);
resolve(); //SOMETHING MORE SUBSTANTIAL GOES HERE
});
console.log("adding pr to worklist");
promiseList.push(pr);
});
}
console.log("Transition *START*-" +promiseList.length +" ");
Promise.all(promiseList).catch(function(error) {
console.log("Part of TransitionCursor Failed!", error);
}).then(this.summarizeWorkDone());
在这两种情况下,输出都像
new Promise...
doing Exit stuff at time 0
new Promise...
doing Exit stuff at time 0
"Transition *START*-"
与预期
new Promise...
new Promise...
"Transition *START*-"
doing Exit stuff at time 0
doing Exit stuff at time 0
如何动态创建承诺列表以供以后执行?
好,
因此人们不赞成我的答案,因为我提供了外部链接,该链接实际上是我写的。 好难过! 因此,修改答案和上一个答案位于该答案的底部。
在这里,我将重点介绍第一个示例中的问题,因为这是我在本文底部的链接中所解释的。
首先:您不是在链接承诺,而是创建多个承诺。 创建诺言时,传递给构造函数的函数将立即被调用。 请在下面的链接中查看教程的第1节以获取详细信息。 这就是为什么您在“新承诺”行之后得到输出行“正在做出口事情……”的原因。 您没有说任何有关curTime的信息,但似乎值不正确,尽管这不是本文的重点。
其次,您应该将lastPr.then()分配给lastPr(),但是您要将新创建的pr分配给lastPr。 在下面的链接的第2节中对此进行了说明。
第三,我想说的是您的预期输出,我应该提醒您,传递给promise构造函数的函数会立即启动。 因此,您不应在promise创建期间打印任何内容,而应在then()方法中将其下推。 但是,我不是在这里执行此操作,因此您将看到第一个“在时间0结束时正在执行出口操作”行紧接在第一个“ new Promise ...”行之后。
第四,您没有显示“ lastPr.then”。 预期产量
注意:我更改了代码的curTime和StatesToExit(删除了“ this”引用并分配了测试数组)
我建议这样的事情:
var curTime = 0;
var lastPr = null;
console.log(" exit setup......................");
var statesToExit = [1, 2, 3]; // NOTE: the change to make this code runnable.
while (statesToExit.length > 0) {
var v = statesToExit.shift();
console.log("new Promise...");
if (lastPr == null) {
lastPr = new Promise(function (resolve, reject) {
console.log("doing Exit stuff at time " + curTime);
resolve(); //SOMETHING MORE SUBSTANTIAL GOES HERE
});
}
else {
console.log("lastPr.then."); // NOTE: This does not appear in your expected output
lastPr = lastPr.then(result => {
console.log("doing Exit stuff at time " + curTime);
return Promise.resolve(); //SOMETHING MORE SUBSTANTIAL GOES HERE
});
}
// console.log("adding pr to worklist");
// promiseList.push(pr);
// });
}
// NOTE: now you need to esecute the chain:
// you should handle promise rejection:
// check the tutorial link:
// https://github.com/tuhinpaul/syp-model/wiki/Programmatic-Chaining-and-Recursive-Functions-with-JavaScript-Promise
lastPr.catch(err => { console.log(err); });
上面代码的输出:
exit setup......................
new Promise...
doing Exit stuff at time 0
new Promise...
lastPr.then.
new Promise...
lastPr.then.
doing Exit stuff at time 0
doing Exit stuff at time 0
我在以下教程中描述了动态的Promise链接:请查看以下教程以了解
我对诺言的理解确实不正确,如以下出色页面所述: http : //pouchdb.com/2015/05/18/we-have-a-problem-with-promises.html首先,它们确实立即开始执行,其次要动态执行,您必须使用工厂函数来推迟执行,如下所示:
首先创建链开始。
var chain = Promise.resolve();
// chain.then(this.doTranStart);
然后为每个状态(一个数组)创建一个代理关闭,以便可以使用适当的数据来回调该函数。 我不确定是否为此内置了JavaScript,因此我从ActionScript 2.0移植了库,但遇到类似问题时又将其移植回来。
this.statesToExit.forEach(function (state) {
console.log("new Exit Promise Chain...");
var pxfn = GetProxy.getProxy(self, self.doExitFactory, [curTime,state._fn,state]);
chain = chain.then(pxfn);
});
然后运行工厂链,该工厂链将呼叫工厂,然后执行承诺。
chain.catch(function(error) {
console.log("Part of TransitionCursor Failed!", error);
}).then(this.summarizeWorkDone);
首先是工厂方法,非常简单。
doExitFactory(curTime,fn,stateScope) {
console.log("doExitFactory " );//+ curTime,fn,stateScope );
return this.doExit(curTime,fn,stateScope);
}
这是要做的实际功能。 在给定范围内动态调用函数。
doExit(curTime, _fn, stateScope) {
console.log("doExit");
return new Promise(function (resolve, reject) {
// console.log("doing actual Exit stuff at time ");//+ curTime);
_fn.apply(stateScope, [{sig: STATE.EXIT}, resolve, reject]);
resolve();
})
}
输出看起来像
TransitionCursor.setRoute
starting to create chain
new Exit Promise Chain...
new Exit Promise Chain...
finishing create chain
calling chain to start............
Transition *START*--------------------------
doExitFactory
doExit
IS_s11 (EXIT)
doExitFactory
doExit
IS_s1 (EXIT)
summarizeWorkDone:---------------
其中IS_s11和IS_s1是我要按顺序运行的实际异步函数。
你写了:
new Promise((r)=>r()).then(new Promise((r)=>r());
您要写的是:
new Promise((r)=>r()).then(()=>new Promise((r)=>r()));
为了使您的代码按您想要的方式工作,请进行更改
lastPr.then(pr);
至
lastPr.then(()=>pr);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.