0

When the someevent event is fired I simply want to wait for the promise to be resolved before continuing. But slowFunctionThatReturnsPromise takes a long time to resolve and the overall process shuts down before it is resolved. Therefore it is never completed. I would have thought using a then would wait for the promise to be resolved, but I can't figure out why it doesn't wait.

emitter.on('someevent', listener)


var listener = function()
{
    x.slowFunctionThatReturnsPromise()
        .then(function()
         {
             console.log('done');
         })
}
jmccure
  • 1,180
  • 7
  • 16
  • *Waiting* and *asynchronous* are things that are opposite to each other. *Synchronous* code will keep the process busy before continuing with any other code in the same call stack, while *asynchronous* code only gets called *after* the currently running call stack is completed. – trincot Mar 11 '17 at 10:13
  • i need the promise returned by `slowFunctionThatReturnsPromise` to be resolved before exiting the `listener` function – jmccure Mar 11 '17 at 10:19
  • 1
    `then` callbacks are executed asynchronously, so by the very nature (and intention) of promises such callbacks will *not* be executed before returning the promise, but after the current code task has completed. So: not possible. – trincot Mar 11 '17 at 10:27
  • not even with `setImmediate`? https://nodejs.org/api/events.html#events_asynchronous_vs_synchronous – jmccure Mar 11 '17 at 10:29
  • 1
    No, again, the part that says *"this happens asynchronously"* will be executed *after* all currently running code has been executed until an empty call stack is reached. Only then the event queue is processed to execute any pending asynchronous code. – trincot Mar 11 '17 at 10:31
  • 1
    For goodness sakes, if this is actually a mocha exiting issue, then please describe the REAL issue and show the REAL code. We ALWAYS do better here if you show the real problem, not some abstracted version of code you created that hides the real problem. – jfriend00 Mar 11 '17 at 15:36

2 Answers2

1

You can't do it on "exit" event which node.js describes

Listener functions must only perform synchronous operations. The Node.js process will exit immediately after calling the 'exit' event listeners causing any additional work still queued in the event loop to be abandoned. In the following example, for instance, the timeout will never occur:

https://nodejs.org/dist/latest-v7.x/docs/api/process.html#process_event_exit

You can though handle graceful kill signal and then exit the process when you're ready.

process.on('SIGINT', () => {
    x.slowFunctionThatReturnsPromise().then(function() {
        process.exit(0);
    })
});
wookieb
  • 4,099
  • 1
  • 14
  • 17
  • apologies, i was using exit as a (bad) example, the event name is actually something else – jmccure Mar 11 '17 at 10:06
  • 1
    Ok. In that case you can't do it either since listeners in standard EventEmitter are synchronous. This event emitter might be better for you. https://www.npmjs.com/package/async-eventemitter – wookieb Mar 11 '17 at 10:19
  • thanks for the link, that would certainly work, but unfortunately the event emitter i am working in is within mocha, and I don't want to modify mocha https://www.npmjs.com/package/mocha – jmccure Mar 11 '17 at 10:22
  • Can you please tell more about what exactly you need to do? How does it relate to mocha? – wookieb Mar 11 '17 at 10:24
  • On test failure https://github.com/mochajs/mocha/blob/master/lib/runner.js#L58 i need to perform a function that takes 10 seconds within the listener function, but the mocha process ends before the slow function does, even when using .then – jmccure Mar 11 '17 at 10:27
  • What this slow function do? Does it need to be run for all failed tests? Can you have custom test reporter that receives information about all failed tests and do something with it? – wookieb Mar 11 '17 at 10:29
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/137817/discussion-between-wookieb-and-jmccure). – wookieb Mar 11 '17 at 10:32
  • Unfortunately it needs to be executed before the cleanup step of each test – jmccure Mar 11 '17 at 10:38
0

Are you missing a return in front of x.slowFunctionThatReturnsPromise()?

Dao Lam
  • 2,837
  • 11
  • 38
  • 44
  • 1
    Yes, but node `EventEmitter`s don't care what the listeners return so this will not affect behaviour. – Bergi Mar 19 '17 at 21:44