I have an array, I need to repile with the use of some edits. I do it with the help of async.concat()
, but something is not working.
Tell me, where is the mistake?
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);
});
In my opinion, everything is logical. callback is invoked immediately after the first iteration. But how can I send the data after the pletion of processing the entire array?
Thank you!
I have an array, I need to repile with the use of some edits. I do it with the help of async.concat()
, but something is not working.
Tell me, where is the mistake?
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);
});
In my opinion, everything is logical. callback is invoked immediately after the first iteration. But how can I send the data after the pletion of processing the entire array?
Thank you!
Share Improve this question edited Jul 17, 2013 at 15:47 Bergi 666k161 gold badges1k silver badges1.5k bronze badges asked Jul 17, 2013 at 14:08 RomanGorRomanGor 4,4952 gold badges20 silver badges26 bronze badges 4-
What does the
console.log
call log? What's unexpected with the result? – Bergi Commented Jul 17, 2013 at 14:18 - @Bergi In the callback es only one element of the array. – RomanGor Commented Jul 17, 2013 at 14:20
-
Ah, that's because you're "throwing" an error. Use the second argument to
callback
instead. – Bergi Commented Jul 17, 2013 at 14:33 - @Bergi look ments for the first answer. – RomanGor Commented Jul 17, 2013 at 14:35
3 Answers
Reset to default 3Instead of callback(dialog);
, you want
callback(null,dialog);
because the first parameter to the callback function is an error object. The reason that console.log(arguments)
is getting called after the first iteration is because async
thinks an error occurred.
I solved the problem but did not understand its meaning. The problem was due to the element which was null instead of the processed value. The program broke off at this point, but do not throw away any errors / warnings.
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);
});
Though I am a little late to post this answer, I see none of us has used .concat function in the way it supposed to be used.
I have created a snippet that says the proper implementation of this function.
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);
}