0

I don't understand how to call a function recursively in node.js for example:

var releaseStock = function (callback) {

  getItems(function (err, items) {
    if (err) {
      return callback(err);
    } else {
      if (items) {
        return callback(items);
      } else {
        setTimeout(function() {
          releaseStock(callback);
        }, 5000); 
      }
    }
  });
};

How can i make it work?

7
  • 1
    You'l need to specify what exactly you're trying to do. As it stands, this question is unanswerable. Commented Sep 30, 2015 at 21:56
  • Sorry, i'll explain better, in this releaseStock method, you will have a method to bring items, so, if you get items, you'll make something with them, make it wait the process for x seconds, and then call releaseStock again. Commented Sep 30, 2015 at 22:01
  • My question is about how to call the method again, with callback or not Commented Sep 30, 2015 at 22:01
  • Please update your question with relevant information rather than commenting here. It may also be helpful to provide some sample input, output, and pseudocode possibly. Commented Sep 30, 2015 at 22:01
  • Add ALL further explanation about your question to the question itself using the "Edit" link. In other words, fix your question so it contains all the necessary information for us to answer. We do not want infomation that is essential to understanding the question to only be present in comments. The question needs to be clear on it's own. Commented Sep 30, 2015 at 22:02

1 Answer 1

2

I'm not entirely sure what you want to do, but I suspect it is something along the lines of:

var releaseStock = function(callback) {

  // get items from somewhere:
  var items = getItems();

  if (!items) {
    // if there are no items, try again (recurse!):
    return releaseStock(callback);
  }

  // if there are items, give them to the callback function:
  return callback(items);
};
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.