Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pause in the middle of a function (delay return) and continue on event

Tags:

node.js

Is there a way to stop in the middle of a function, and wait for a certain event before continuing?

like image 629
Michael Andersen Avatar asked Nov 15 '10 15:11

Michael Andersen


2 Answers

Unfortunately I don't think it can be done. Once you begin execution of a function it will continue until the termination of the function. I believe what you might want to try is create two functions with a callback to the second function that is registered with the event you're listening for. However, it's tough to know that for sure since you haven't posted any code.

like image 70
Brian Driscoll Avatar answered Nov 10 '22 00:11

Brian Driscoll


You should wrap the second half of your code in an anonymous function and attach it as an event handler:

function myFunction() {
    firstHalfOfMyFunction();
    eventEmitter.on('someEvent', function () {
        secondHalfOfMyFunction();
    });
}
like image 34
ngn Avatar answered Nov 10 '22 00:11

ngn