Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

NodeJS checking for function completion before proceeding

NodeJS checking for function completion before proceeding

Problem

I have a question regarding the asynchronous method APIs in Node.

So, I have a Function, let's call it longSetup(callback).

As the name suggests, longSetup(callback) takes a long time to complete. However, this setup can be done in parallel with other things. So, while longSetup() is happening, I can have other things being setup as well.

So once longSetup() is done, that class is ready to execute another function, such as serveData().

Now, I don't know when serveData() will be invoked. serveData() is based on user I/O, and may be invoked before longSetup() finishes, or may be invoked after longSetup() finishes. Also, I never directly invoke serveData().

It's true that I need longSetup() to be completed before serveData() can be executed successfully, but I'd like to capitalize on the fact that longSetup() can be done asynchronously, and thus I don't want to implement a synchronous version of longSetup(); I'd rather have other setup being done during that time.

Since serveData() can be called anytime, I'd like to check if longSetup() is completed when serveData() is invoked. If it completed, proceed with serveData(). If not, wait till longSetup() is done and then proceed with execution. Since longSetup() is invoked only once, I won't be waiting in most cases. Also, if user I/O takes time, longSetup() would be done by the time serveData() is invoked for the first time as well.

I've been through a couple of SO questions and answers regarding waiting for a function to return before executing another function/firing one function after another function etc. and I understand the asynchronous way of Node and why to use callbacks. However, this is a different use case and I was wondering if node provides some way of waiting on another asynchronous function.

Thanks all!

Problem courtesy of: codeBearer

Solution

@arturgrzesiak's answer was calling serveData from longSetup and had no way to delay execution of serveData until longSetup was complete. edit upon reading his answer more closely it looks like i'm mistaken, and serveData would be called once at the end of longSetup. however my solution should allow serveData to be called multiple times if e.g. multiple users request data during setup.

var setupFinished = false;

var longSetup = function() {
    ...
    // at end:
    setupFinished = true;
}

var serveDataWrapper = function(query){
  if(!setupFinished){
    setTimeout(
      function(){
        serveDataWrapper(query);
      },
      200 //ms
    );
  } else { // setup is finished
    serveData(query);
  };
}

var serveData = function (query) {
  // longSetup is now guranteed to be complete
};

app.get('/', function(req, res){
  serveDataWrapper(req.query['query']);
});
Solution courtesy of: Plato

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

NodeJS checking for function completion before proceeding

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×