2

I have an array, I need to recompile 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 completion of processing the entire array?

Thank you!

4
  • What does the console.log call log? What's unexpected with the result? Commented Jul 17, 2013 at 14:18
  • @Bergi In the callback comes only one element of the array. Commented Jul 17, 2013 at 14:20
  • Ah, that's because you're "throwing" an error. Use the second argument to callback instead. Commented Jul 17, 2013 at 14:33
  • @Bergi look comments for the first answer. Commented Jul 17, 2013 at 14:35

3 Answers 3

3

Instead 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.

Sign up to request clarification or add additional context in comments.

4 Comments

I think it is not fundamentally important. But even with that there is a problem, if I pass the callback in the first argument null, nothing inside callback it does not work, even console.log(arguments);
@RomanGorbatko: The documentation also says that the second value that is passed to the callback should be an array. It looks like dialog is an object?
No, dialog is a object. But, if done well: callback (null, [dialog]), we get the same thing - nothing
If I as the first argument is null then the inside callback, nothing happens, but if you pass something like 123 - works.
2

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

7 Comments

You always have to call the callback function passed by async to your handler function. In your initial example, you didn't call it if dialog['viewer']['user_profile_image'] happened to be null. That will probably have resulted in undefined behavior.
Why are you using async.concat() here and not async.map()?
@LeonidBeschastny Because I need to collect all of the elements of the array into one. But, frankly, I do not quite understand the difference between .concat and .map.
@robertklep I was confused what node.js did not throw any error.
async.map maps each element of an array into a new one, resulting in a new array of the same length. async.concat maps each element of an array into an array of sub-elements and then concatenates all them into one array. So, with async.concat you can produce an array of any length, while async.map will always produce an array of the same length as the original one.
|
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.