How to pass the value of the request.get to the callback using async.series node.js

403 views Asked by At

I wanna show my code:

    request({url: API_URL}, function (error, response, body) {

    if (!error && response.statusCode == 200) {

        console.log("Items received with success.");
        var data = JSON.parse(body);

        // catch duration from video
        for(var list in data) {
            var item = data[list];

            async.series([
                function(callback){
                    console.log("I'm inside fileExists function");
                    var result = '';
                    request.get('http://tobeymotos.com.br/video.mp4', function (error, response, body) {
                        if (!error && response.statusCode == 200) {
                            var video = body;
                            console.log("File exists and is OK");
                            var result = true;
                            console.log("Valor da result", result);
                        }
                        else{
                            console.log("This file do not exists, check error: "+ error);
                            var result = "xixi";
                        }
                    });
                    // usar aqui
                    callback(null, result);
                },
                function(callback){
                    // catch video duration
                    file = "http://tobeymotos.com.br/video.mp4"; // used as e.g.
                    console.log("I'm inside getVideoDuration function");
                    getDuration(file).then(function (duration) {
                        console.log("Duração do vídeo: " +duration);
                        return duration;
                    });
                    callback(null, 'b');
                },
                function(callback){
                    // code c
                    callback(null, 'c');
                },
                function(callback){
                    // code d
                    callback(null, 'd');
            }],
            // optional callback
            function(err, results){
                console.log(results);
            }
            )

            return 0;           
        }
    } else {
        console.log("Error: "+error);
    }
});

I'm using async.series to perform some functions in sequence, because I depend on the result of the first one, to continue with the next.

As you can see I have a "request.get" in the first function, where:

request.get('http://tobeymotos.com.br/video.mp4', function (error, response, body) {
                    if (!error && response.statusCode == 200) {
                        var video = body;
                        console.log("File exists and is OK");
                        var result = true;
                        console.log("Valor da result", result);
                    }
                    else{
                        console.log("This file do not exists, check error: "+ error);
                        var result = "xixi";
                    }
                });
                // usar aqui
                callback(null, result);

The problem is that I can not call result in the callback, it just does not "exit" the request.get

Some help?

1

There are 1 answers

0
chris-tkb On BEST ANSWER
request({url: API_URL}, function (error, response, body) {

    if (!error && response.statusCode == 200) {

        console.log("Items received with success.");
        var data = JSON.parse(body);

        // catch duration from video
        for(var list in data) {
            var item = data[list];

            async.series([
                function(callback){
                    console.log("I'm inside fileExists function");
                    var result = '';
                    request.get('http://tobeymotos.com.br/video.mp4', function (error, response, body) {
                        if (!error && response.statusCode == 200) {
                            var video = body;
                            console.log("File exists and is OK");
                            var result = true;
                            console.log("Valor da result", result);
                            callback(null, result);
                        }
                        else{
                            console.log("This file do not exists, check error: "+ error);
                            var result = "xixi";
                            callback(null, result);
                        }
                    });
                    // usar aqui

                },
                function(callback){
                    // catch video duration
                    file = "http://tobeymotos.com.br/video.mp4"; // used as e.g.
                    console.log("I'm inside getVideoDuration function");
                    getDuration(file).then(function (duration) {
                        console.log("Duração do vídeo: " +duration);
                        return duration;
                    });
                    callback(null, 'b');
                },
                function(callback){
                    // code c
                    callback(null, 'c');
                },
                function(callback){
                    // code d
                    callback(null, 'd');
            }],
            // optional callback
            function(err, results){
                console.log(results);
            }
            )

            return 0;           
        }
    } else {
        console.log("Error: "+error);
    }
});

Your callback is outside of the request.get function, this is possibly causing your callback to call before the request has returned due to the nature of Node JS. Node JS is non-blocking and so does not wait for a function to be carried out before moving on to the next unless explicitly told to. Putting the callback inside of the request.get response function will force it to wait for the request to return and then carry out the callback with the result