簡體   English   中英

async.concat() 之后的 Node.js concat 數組

[英]Node.js concat array after async.concat()

我有一個數組,我需要使用一些編輯重新編譯。 我是在async.concat()的幫助下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);
});

在我看來,一切都是合乎邏輯的。 在第一次迭代后立即調用回調。 但是如何在處理完整個數組后發送數據呢?

謝謝!

而不是callback(dialog); ,你要

callback(null,dialog);

因為回調函數的第一個參數是一個錯誤對象。 在第一次迭代后調用console.log(arguments)的原因是因為async認為發生了錯誤。

我解決了問題,但不明白其含義。 問題是由於元素為 null 而不是處理過的值。 程序此時中斷,但不要丟棄任何錯誤/警告。

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);
});

雖然我發布這個答案有點晚了,但我發現我們沒有人以它應該使用的方式使用 .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); }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM