繁体   English   中英

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM