且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在递归异步函数中承诺

更新时间:2022-10-19 12:55:38

Simply return the promise returned by .then and either return the final value from the .then callback, or another promise:

function getParents(bookmark, parents) {
  var parents = parents || [];

  return getParent(bookmark).then(function(parent) {
    parents.push(parent);
    return parent.parentId == '0' ? parents : getParents(parent, parents);
  });
}

See http://www.html5rocks.com/en/tutorials/es6/promises/#toc-chaining