Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js NodeJS Async:无法理解流_Node.js_Mongodb_Asynchronous_Mlab_Node Async - Fatal编程技术网

Node.js NodeJS Async:无法理解流

Node.js NodeJS Async:无法理解流,node.js,mongodb,asynchronous,mlab,node-async,Node.js,Mongodb,Asynchronous,Mlab,Node Async,现在,一旦我收集了所有9个数组中的数据,我需要对其进行处理。但是,只有当所有9个数组都填满了关于驱动程序的数据时,才能做到这一点 但是我不确定从何处调用process_arrays函数,该函数仅在async.each完成后处理所有数组。第三个参数第二个函数不只是用于错误。它用于在迭代完成或失败后指定任何延续,例如调用process\u数组 不过,您还需要在迭代器函数apicall…中调用cb,以获得成功和失败。如果不这样做,async.each将不会继续到集合中的下一个值 async.each(

现在,一旦我收集了所有9个数组中的数据,我需要对其进行处理。但是,只有当所有9个数组都填满了关于驱动程序的数据时,才能做到这一点


但是我不确定从何处调用process_arrays函数,该函数仅在async.each完成后处理所有数组。

第三个参数第二个函数不只是用于错误。它用于在迭代完成或失败后指定任何延续,例如调用process\u数组

不过,您还需要在迭代器函数apicall…中调用cb,以获得成功和失败。如果不这样做,async.each将不会继续到集合中的下一个值

async.each(
    driver,
    function(apiRequest, cb) {
        apicall(apiRequest, cb);
    },
    function(err) {
        if (err) console.log("error...", err);
        else process_arrays();
    }
);

您为apicall函数定义了一个名为cb的回调函数,但实际上从未调用过它。因此,apicall从不在完成或出现错误时通知任何人。那是行不通的。当异步操作完成或出现错误时,您必须调用回调。现代的方法是使用请求承诺库而不是请求,然后使用承诺来监视异步操作的完成,而不是异步库。谢谢@jfriend00Yup,它起到了作用。谢谢你的详细解释
async.each(
    driver,
    function(apiRequest, cb) {
        apicall(apiRequest, cb);
    },
    function(err) {
        if (err) console.log("error...", err);
        else process_arrays();
    }
);
function apicall(item, cb) {
    request(
        'https://...',
        function(error, response, body) {
            if (error || response.statusCode !== 200) {
                // argument means failure
                cb(error || new Error(response.statusCode));
            } else {
                console.log("----->" + body);
                var o = JSON.parse(body);

                // for loop ...

                // no argument means success
                cb();
            }
        }
    );
}