在async.concat()之后的Node.js连接数组。

问题描述 投票:2回答:3

我有一个数组,我需要使用一些编辑来重新编译。我借助于 async.concat()但有些东西是不工作的.告诉我,哪里出了问题?

async.concat(dialogs, function(dialog, callback) {
    if (dialog['viewer']['user_profile_image'] != null) {
        fs.exists(IM.pathToUserImage + dialog['viewer']['user_profile_image'].replace('%s', ''), function(exits) {
            if (exits) {
                dialog['viewer']['user_profile_image'] = dialog['viewer']['user_profile_image'].replace('%s', '');
            }
            callback(dialog);
        });
    }
}, function() {
    console.log(arguments);
});

在我看来,一切都符合逻辑,回调是在第一次迭代后立即调用的,但如何在处理完整个数组后再发送数据呢?但如何在处理完整个数组后再发送数据?

谢谢你!我有一个数组,我需要在处理完整个数组后立即调用回调。

javascript node.js async.js
3个回答
3
投票

而不是 callback(dialog);,你要

callback(null,dialog);

因为回调函数的第一个参数是一个错误对象。 原因是 console.log(arguments) 在第一次迭代后被调用,是因为 async 认为发生了错误。


2
投票

我解决了这个问题,但不明白其含义。问题是由于元素是空的,而不是处理后的值。这时程序断了,但不要扔掉任何错误警告。

async.map(dialogs, function(dialog, callback) {
    if (dialog['viewer']['user_profile_image'] == null) {
        dialog['viewer']['user_profile_image'] = IM.pathToUserImage;
    }
    fs.exists(IM.pathToUserImage + dialog['viewer']['user_profile_image'].replace('%s', ''), function(exits) {
        if (exits) {
            dialog['viewer']['user_profile_image'] = dialog['viewer']['user_profile_image'].replace('%s', '');
        }
        callback(null, dialog);
    });
}, function(err, rows) {
    if (err) throw err;
    console.log(rows);
});

0
投票

虽然我发布这个答案有点晚,但我看到我们都没有按照它应该使用的方式使用.concat函数。

我创建了一个片段,说了这个函数的正确实现方法。

let async = require('async');
async.concat([1, 2, 3], hello, (err, result) => {
    if (err) throw err;
    console.log(result); // [1, 3]
});

function hello(time, callback) {
    setTimeout(function () {
        callback(time, null)
    }, time * 500);
}
© www.soinside.com 2019 - 2024. All rights reserved.