Michael Andersen
Michael Andersen

Reputation: 989

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

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

Upvotes: 5

Views: 3263

Answers (3)

Oleg
Oleg

Reputation: 613

You can use node-fibers which I use in my Common Node library.

Upvotes: 2

ngn
ngn

Reputation: 7892

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();
    });
}

Upvotes: 2

Brian Driscoll
Brian Driscoll

Reputation: 19635

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.

Upvotes: 6

Related Questions