Node.js 回调和递归

Node.js callbacks and recursion

我不明白如何在 node.js 中递归调用一个函数,例如:

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

我怎样才能让它发挥作用?

我不完全确定你想做什么,但我怀疑是这样的:

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