可选的回调不被称为Node.js的异步模块的forEachOf方法 [英] Optional callback not being called in the node.js async module's forEachOf method

查看:550
本文介绍了可选的回调不被称为Node.js的异步模块的forEachOf方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是异步模块的forEachOf方法来打印通过对象迭代后的最终结果。下面是我在做什么的简化版本:

I'm using the async module's forEachOf method to print the end result after iterating through an object. Here is a shortened version of what I'm doing:

var async = require('async'),
    cheerio = require('cheerio'),
    request = require('request');

var returnArray = [];

async.forEachOf(myObj, function (value, key, callback) {
    var anotherObj = {};

    anotherObj.type = "val1";

    request(someurl, function(err, res, body) {
        if (err) {
            return callback(err);
        }

        var $ = cheerio.load(body);

        anotherObj.name = "val2";

        var miniObj = {};
        $('#some-element', "#an-id").each(function(i, value) {
            var val = value.value;
            miniObj[size] = val;
        });

        anotherObj.miniObj = miniObj;

        returnArray.push(anotherObj);

        return callback();
    });
}, function (err) {
    if (err) {
        console.error(err.message);
    }

    console.log(returnArray);
});

然而,当我运行程序,没有什么(即returnArray')被打印到控制台像它应该的。

However, when I run the program, nothing (namely, 'returnArray') gets printed to the console like it should be.

有关参考,我已经看过这些其他类似的帖子:

For reference, I have looked at these other, similar posts:

  • Using async module to fire a callback once all files are read (seems outdated, is using the wrong method, and doesn't address the issue)
  • Last callback not being called using async (doesn't address the issue)

我不知道我在做什么错在这里。任何人都可以请指出我在做什么错?

I'm not sure what I'm doing wrong here. Could anyone please point out what I'm doing wrong?

谢谢!

编辑:所以我觉得我终于想通了,我在做什么错。在不同的例子我先前提供的在这里,用了Runnable 我忘了补充一个回归回调()语句。然后它的工作。这一点,本实施例是,在上述提供的例子我的'返回回调()'语句本身另一个异步方法中调用的唯一区别。我认为,为了解决我的问题,我会以某种方式必须确保(可能使用异步的图书馆的一些控制流功能)来调用回归回调()在第二异步方法完成后正确的范围/级别。我想我会这样,我的错误,归因于缺乏文档上的返回回调()语句中的异步文档的正确用法。我会更新这个帖子了一个解决方案,一旦我弄明白(赶上一些睡眠)!

So I think I finally figured out what I was doing wrong. In a different example I provided earlier HERE, using Runnable I forgot to add a 'return callback()' statement. Then it worked. The only difference between that and this example being that my 'return callback()' statement in the above-provided example is itself called within another asynchronous method. I think in order to fix my problem I will somehow have to make sure (probably using some control flow function in async's library) to call 'return callback()' at the correct 'scope/level' after the second asynchronous method has finished. I think I will attribute this, my mistake, to the lack of documentation on proper usage of the 'return callback()' statement in the async docs. I will update this post with a solution once I figure it out (and catch up on some sleep)!

推荐答案

您的语句:

if (err) {
    return callback(err);
}

不是有效的异步编程。相反,你应该做的:

is not valid for asynchronous programming. Instead, you should do:

if(err) callback(err);

这就是为什么你没有得到任何东西回来。我改写了你的code。与异步应用概念:

This is why you aren't getting anything returned. I rewrote your code with async concepts applied:

var async = require('async'),
var cheerio = require('cheerio'),
var request = require('request');

var returnArray = [];
async.forEachOf(myObj, function (value, key, next) {
    var anotherObj = {};

    anotherObj.type = "val1";

    request(someurl, function(err, res, body) {
        if (err) next(err);

        var $ = cheerio.load(body);

        anotherObj.name = "val2";

        var miniObj = {};
        async.each($('#some-element', "#an-id"), function (value, next) {
          var val = value.value;
          miniObj[size] = val;
        });

        anotherObj.miniObj = miniObj;

        returnArray.push(anotherObj);

        next();
    });
}, function (err) {
    if (err) console.error(err.message);

    console.log(returnArray);
    callback(returnArray);

});

你有两个不同的命名回调通知。外回调函数被调用回调。内部函数调用回调接下来

这篇关于可选的回调不被称为Node.js的异步模块的forEachOf方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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