简体   繁体   English

将函数传递给Node.js中的嵌套回调

[英]Passing function to nested callback in Node.js

I'm trying to get my head around how to perform a couple of async request but passing the a function to the callback. 我试图弄清楚如何执行几个异步请求,但是将a函数传递给回调。 Like below in my GetPosts(), I'm passing a function. 就像下面我的GetPosts()中一样,我正在传递一个函数。 So my question is how do I handle the callbacks in the async.series function at the same time as being able to pass in a function to deal with callback from these callbacks from the series. 所以我的问题是我如何在处理async.series函数中的回调的同时传递一个函数来处理来自系列中这些回调的回调。 At the moment I'm calling cb but not the "callback", but not sure what the work around is. 目前,我正在呼叫cb而不是“回调”,但是不确定解决方法是什么。 Does that make sense? 那有意义吗?

var request = require("request");
var async = require("async");

Context.prototype.GetPosts = function (params, cb) {
    async.series({
        one: function (callback) {
            request("http://jsonplaceholder.typicode.com/posts/1", function (error, response, body) {
                if (!error && response.statusCode == 200) {
                    return cb(body);
                }
                cb(null, result);
            });
        },
        two: function (callback) {
            request("http://jsonplaceholder.typicode.com/posts/1", function (error, response, body) {
                if (!error && response.statusCode == 200) {
                    return cb(body);
                }
                cb(null, result);
            });
        }
    },
    function (err, results) {
    });
};

var params = new Params("MyPosts");

Context.GetPosts(params, function (cb) {
    console.log("Response: " + cb);
});

Thank you in advance. 先感谢您。

You call callback where you're now calling cb, to let Async know it's time to move on, and then you call cb in the final callback at the bottom of the GetPosts function, as that fires when the entire series is done. 您在现在要调用cb的地方调用callback ,以让Async知道该继续进行了,然后在GetPosts函数底部的最终回调中调用cb,因为整个系列完成后都会触发。

Pass the errors on, so you can handle them in the callback for GetPosts 传递错误,因此您可以在GetPosts的回调中处理它们

var request = require("request");
var async   = request("async");

Context.prototype.GetPosts = function (params, cb) {
    async.series({
        one: function (callback) {
            request("http://jsonplaceholder.typicode.com/posts/1", function (error, response, body) {
                if (error) return callback(error, null);
                if (response.statusCode == 200) return callback(null, body);
                return callback('err', null);
            });
        },
        two: function (callback) {
            request("http://jsonplaceholder.typicode.com/posts/1", function (error, response, body) {
                if (error) return callback(error, null);
                if (response.statusCode == 200) return callback(null, body);
                return callback('err', null);
            });
        }
    },
    function (err, results) {
        if (err) {
            return cb(err, null);
        } else {
            return cb(null, results);
        }
    });
};

var ctx = new Context();

ctx.GetPosts(new Params("MyPosts"), function (err, data) {
    if (err) {
        // handle error
    } else {
        console.log("Response: " + data);
    }        
});

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

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