关于node.js中async.waterfall的问题 [英] questions about async.waterfall in node.js

查看:126
本文介绍了关于node.js中async.waterfall的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am confused about how to use async.waterfall method to clean up my callbacks. I have a couple functions that are making API calls and returning back results from those API calls via callbacks. I want to pass the results from one api call to the next. I'd also ideally like to enclose these calls in separate functions rather than paste them directly into the async.waterfall control flow (for readability).

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

I can't quite figure out if you can call a function that has a callback in it, and it will wait for the callback before going to the next function, or not. Also, when the API SDK requires a callback, do I name it to match the callback name in the async.waterfall (in this case called 'callback')?

我可能将很多东西混合在一起。希望有人可以帮助我解决这个问题。下面是我要执行的操作的部分代码段。

I may be mixing a lot of things together. Hopefully someone can help me unravel this. Below is a partial code snippet of what I'm trying to do...

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):

If one of your api calls returns successfully, you call the local callback with null as the first argument. Otherwise, call it with an error, and async.waterfall is going to stop the execution and run the final callback. For example (didn't test 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")
    });
};

这篇关于关于node.js中async.waterfall的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆