繁体   English   中英

关于node.js中async.waterfall的问题

[英]questions about async.waterfall in node.js

我对如何使用async.waterfall方法清理回调感到困惑。 我有几个函数可以进行API调用,并通过回调从这些API调用返回结果。 我想将结果从一个api调用传递到下一个。 理想情况下,我也想将这些调用包含在单独的函数中,而不是将它们直接粘贴到async.waterfall控制流中(以提高可读性)。

我不太清楚您是否可以调用其中包含回调的函数,并且该函数将等待回调,然后再转到下一个函数。 另外,当API SDK需要回调时,是否将其命名为与async.waterfall中的回调名称相匹配(在这种情况下称为“回调”)?

我可能将很多东西混合在一起。 希望有人可以帮助我解决这个问题。 以下是我正在尝试做的部分代码片段...

async.waterfall([
  function(callback){
    executeApiCallOne();
    callback(null, res);
  },
  function(resFromCallOne, callback){
    executeApiCallTwo(resFromCallOne.id);
    callback(null, res);
  }],
  function (err, res) {
    if(err) {
        console.log('There was an error: ' + err);
    } else {
        console.log('All calls finished successfully: ' + res);
    }
  }
);

//API call one
var executeApiCallOne = function () {

    var params = {
      "param1" : "some_data",
      "param2": "some_data"
      };

    var apiClient = require('fakeApiLib');
    return apiClient.Job.create(params, callback);
    // normally I'd have some callback code in here
    // and on success in that callback, it would
    // call executeApiCallTwo
    // but it's getting messy chaining these
};

//API call two
var executeApiCallTwo = function (id) {

    var params = {
      "param1" : "some_data",
      "param2": "some_data",
      "id": id
      };

    var apiClient = require('fakeApiLib');
    // do I name the callback 'callback' to match
    // in the async.waterfall function?
    // or, how do I execute the callback on this call?
    return apiClient.Job.list(params, callback);
};

如果您的一个api调用成功返回,您将以null作为第一个参数调用本地回调。 否则,请使用错误进行调用,并且async.waterfall将停止执行并运行最终的回调。 例如(没有测试ID):

async.waterfall([
  function(callback){
    executeApiCallOne(callback);
  },
  function(resFromCallOne, callback){
    executeApiCallTwo(resFromCallOne.id, callback);
  }],
  function (err, res) {
    if(err) {
        console.log('There was an error: ' + err);
    } else {
        // res should be "result of second call"
        console.log('All calls finished successfully: ' + res);
    }
  }
);

//API call one
var executeApiCallOne = function (done) {

var params = {
  "param1" : "some_data",
  "param2": "some_data"
  };

var apiClient = require('fakeApiLib');
    return apiClient.Job.create(params, function () {
        // if ok call done(null, "result of first call");
        // otherwise call done("some error")
    });
};

//API call two
var executeApiCallTwo = function (id, done) {

    var params = {
      "param1" : "some_data",
      "param2": "some_data",
      "id": id
      };

    var apiClient = require('fakeApiLib');
    // do I name the callback 'callback' to match
    // in the async.waterfall function?
    // or, how do I execute the callback on this call?
    return apiClient.Job.list(params, function () {
        // if ok call done(null, "result of second call");
        // otherwise call done("some error")
    });
};

暂无
暂无

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

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