0

我有一个进行 api 调用的函数和第二个函数,它遍历第一个函数的数据并在每次迭代时调用一个 api。我正在尝试使用异步库来实现这一点,但第二个函数仍在异步运行,而不是等待完成。所以我最终运行函数 1 运行,函数 2 启动,但最终回调在函数 2 完成之前运行。

async.series([
        function (callback) {
          //api call
          getShelves.execute(function (err, shelves) {
            if (err) { return callback(err); }
            async.forEach(shelves.items, function (shelf, callback) {
              var shelfObj = {id: shelf.id, title: shelf.title, books: []};
              bookShelves.push(shelfObj);
              callback();
            });

            //sort numerically to make placing books easier
            bookShelves.sort(function (a, b) {return a.id - b.id; });
            callback();
          });
        },
        function (callback) {
          async.forEach(bookShelves, function (shelf, callback) {
            //api call
            getBooks.execute(function (err, books) {
              if (err) { return callback(err); }
              if (books.items) {
                async.forEach(books.items, function (book, callback) {
                  var bookObj = {title: book.volumeInfo.title};
                  bookShelves[shelf.id].books.push(bookObj);
                  callback();
                });
              }
              callback();
            });
          });
          callback();
        }
      ], function (err) {
        if (err) { console.log('error'); }
        res.render('collection', { shelves: bookShelves });
      });
    });

编辑:现在工作谢谢大家

function (callback) {
          async.forEach(bookShelves, function (shelf, callback) {
            getBooks.execute(function (err, books) {
              if (err) { return callback(err); }
              if (books.items) {
                async.forEach(books.items, function (book, callback) {
                  var bookObj = {title: book.volumeInfo.title};
                  bookShelves[shelf.id].books.push(bookObj);
                  console.log(book.volumeInfo.title);

                  //callback to continue book loop
                  callback();
                }, function () {
                  //callback to continue shelf loop
                  callback();
                });
              }else{
                callback();
               }
            });
          }, function () {
            //callback to end function and move to next. However this is never reached
            callback();
          });
        }
4

2 回答 2

1

系列中的第二个函数立即调用它的回调,而不是等到async.forEach迭代完成。相反,请尝试在之后调用它:

    function (callback) {
      async.forEach(bookShelves, function (shelf, callback) {
        //api call
        //... skipped ...
      }, function() {
          callback();
      });
    }
于 2013-09-22T19:06:06.073 回答
0
function loadShelf(shelf, callback) {
  //_.pick is handy for this FYI
  var shelfObj = {id: shelf.id, title: shelf.title};
  //presumably your getBooks call takes a shelf id to relate the
  //getBooks is an asynchronous DB or API call presumably
  getBooks(shelf.id, function (error, books) {
    if (error) {
      callback(error);
      return;
    }
    //This is an in-memory array. No async needed.
    shelfObj.books = books.map(function (book) {
      return {title: book.volumeInfo.title};
    });
    callback(null, shelfObj);
  });
}

getShelves.execute(function (error, dbShelves) {
  if (error) {
    res.render('error', error); //pseudo-code error handling
    return;
  }
  async.each(dbShelves, loadShelf, function (error, fullShelves) {
    if (error) {
      res.render('error', error); //pseudo-code error handling
      return;
    }
    //sort numerically to make placing books easier
    var sortedShelves = fullShelves.sort(function (a, b) {return a.id - b.id; });
    res.render('collection', { shelves: sortedShelves });
});
于 2013-09-22T19:15:50.230 回答