代码之家  ›  专栏  ›  技术社区  ›  Rafael Souza

如何传递请求的值。使用异步获取回调。series node.js

  •  0
  • Rafael Souza  · 技术社区  · 8 年前

    我想展示我的代码:

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

    我正在使用 async.series 表演 functions 按顺序,因为我依赖于第一个的结果,所以继续下一个。

    正如你所见,我有一个“ request.get “首先 function ,其中:

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

    问题是,我无法在 callback ,它只是没有” exit “那 request.get

    需要帮忙吗?

    1 回复  |  直到 8 年前
        1
  •  0
  •   Meyer SQLRocker    8 年前
    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);
        }
    });
    

    您的回调超出了请求范围。get函数,由于Node JS的性质,这可能会导致回调在请求返回之前调用。Node JS是非阻塞的,因此在转移到下一个函数之前不会等待函数被执行,除非明确告知。将回调放在请求中。get响应函数将强制它等待请求返回,然后使用结果执行回调