0

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. 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(). 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?

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. I haven't tried it. Crazy?

Community
  • 1
  • 1
BaldEagle
  • 918
  • 10
  • 18
  • 1
    If you don't want to post a code wall here, could you post your code in a fiddle? It's very difficult to help without code. – TbWill4321 Dec 16 '15 at 18:06
  • A valid point, of course. The fiddle option isn't possible for other reasons. Sorry. And thanks anyway. – BaldEagle Dec 16 '15 at 20:41

1 Answers1

0

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?

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. 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. 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(). 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.

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

var someMorePromises = [...]
someMorePromises.push(pAll);
Promise.all(someMorePromoises).then(...)
jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • You offered two models of async control. I've used your preferred one. I more often use "someAsync().then(function(return, error) {if (error) ...; return someOtherAsync()}).then( ... ). Good point, though. – BaldEagle Dec 16 '15 at 20:33
  • @BaldEagle - Do you get that we can't tell you precisely what's wrong with your code without seeing the relevant portion of the code? So, without your code, all we can do is offer wild guesses? – jfriend00 Dec 16 '15 at 20:37
  • In my recursion, the same function is always involved; no other in the chain. Call it a(). Every resolve() to a() looks the same; I'm not aware of any ability to resolve() to the first one before doing resolve() to the last one so that it can resolve() to the one before ... down to the first one. Yet, code which should depend on completion of the first call is starting immediately after the first resolve() issued. – BaldEagle Dec 16 '15 at 20:38
  • I totally get it. I'm frustrated with this code. I don't mean to pass the frustration to you. So sorry. – BaldEagle Dec 16 '15 at 20:42
  • @BaldEagle - We can help you further IF you post the relevant code. We cannot help you further if you do not post the relevant code. It is as simple as that. – jfriend00 Dec 16 '15 at 22:03