简体   繁体   English

Javascript,节点,承诺和递归

[英]Javascript, Node, Promises, and recursion

I'm having trouble controlling execution flow. 我无法控制执行流程。 This is a follow-on to node.js, bluebird, poor control of execution path and node.js table search fails with promises in use . 这是node.js,bluebird,对执行路径的不良控制以及node.js表搜索因使用promises而失败的后续措施。 Judging by console.log print-outs, my recursive routine works great, except that the first call to resolve() (a signal to the nth recursive call) gives the green light to follow-on code that shouldn't get that green light until the first call to the recursive routine calls resolve(). 从console.log打印输出来看,我的递归例程工作得很好,除了对resolve()的第一个调用(第n个递归调用的信号)使后续代码的绿灯亮了,该代码不应获得绿灯直到对递归例程的第一次调用都调用resolve()。 It turns out the first call to the recursive routine delivers the answer I want reported, but by the time it reports it, the follow-on code is no longer listening for it and is running blissfully along with an "undefined" answer. 事实证明,对递归例程的第一次调用提供了我想要报告的答案,但是到报告它时,后续代码不再侦听它,并且与“未定义”答案一起运行。 Bad. 坏。

My code is much to long to share here. 我的代码在这里很长要分享。 I tried to write a small model of the problem, but haven't found the combination of factors to replicate the behavior. 我试图编写一个小问题模型,但没有发现多种因素的组合来复制行为。

Sound familiar? 听起来有点熟? How do you keep proper control over Promises releasing follow-on code on time? 您如何对Promises准时发布后续代码保持适当的控制?

I thought maybe the first call to the routine could start an array passed into a Promise.all and later calls would add another entry to that array. 我以为可能是对例程的第一次调用可以启动一个传递到Promise中的数组。所有后来的调用都会向该数组添加另一个条目。 I haven't tried it. 我还没试过 Crazy? 疯?

Without seeing your actual code, we can't answer specifically. 如果没有看到您的实际代码,我们将无法具体回答。

Sound familiar? 听起来有点熟? How do you keep proper control over Promises releasing follow-on code on time? 您如何对Promises准时发布后续代码保持适当的控制?

The answer is always to not resolve the first promise in the chain until you're ready for things to execute and to structure your promise chain so that dependent things don't get executed until the things they are waiting on have been properly resolved. 答案总是要在准备好要执行的事物之前就不解决链中的第一个promise,并构造您的promise链,以使从属的事物直到正确解决了他们正在等待的事物才得以执行。 If something is executing too soon, then you're either calling something too soon or your promise structure is not correct. 如果某件事执行得太早,那么您要么调用得太早,要么您的诺言结构不正确。 Without seeing your actual code, we cannot know for sure. 如果没有看到您的实际代码,我们将无法确定。

A common mistake is this: 一个常见的错误是:

someAsyncOperation().then(someOtherAync()).then(...)

which should be: 应该是:

someAsyncOperation().then(someOtherAync).then(...)

where you should pass a reference to the next async function rather than calling it immediately and passing its return value. 您应该在其中传递对下一个异步函数的引用,而不是立即调用它并传递其返回值。

I thought maybe the first call to the routine could start an array passed into a Promise.all and later calls would add another entry to that array. 我以为可能是对例程的第一次调用可以启动一个传递到Promise中的数组。所有后来的调用都会向该数组添加另一个条目。 I haven't tried it. 我还没试过 Crazy? 疯?

You cannot pass an array to Promise.all() and then add things to the array later - that is not a supported capability of Promise.all() . 您不能将数组传递给Promise.all() ,然后再将其添加到该数组中-这不是Promise.all()支持的功能。 You can chain subsequent things onto the results of Promise.all() or do another Promise.all() that includes the promise from the previous Promise.all() and some more promises. 您可以将后续内容链接到Promise.all()的结果上,也可以执行另一个Promise.all() ,其中包括上一个Promise.all()承诺和更多承诺。

var someArrayOfPromises = [...];
var pAll = Promise.all(someArrayOfPromises);

var someMorePromises = [...]
someMorePromises.push(pAll);
Promise.all(someMorePromoises).then(...)

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

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