我无法将异步函数变量返回到node.js中的调用函数

时间:2014-03-25 23:35:51

标签: javascript node.js asynchronous express

//此函数将接收两组数组,其中包含youtube网址,并将依次调用youtube oembed api并接收iframe和视频属性,一切正常,我能够从youtube api接收数据并将其转换为数组,但我无法传递给调用函数

 function getWaterfall(data){
        var resultArr = [];
         function iter(datas, callback){    
        async.waterfall([
           function getData(callback){
           var link = nconf.get('youtubeuri')+encodeURIComponent(datas.src);
           console.log(link);
             http.get(link, function(res) {
            console.log("Got response: " + res.statusCode);
            res.on("data", function(chunk) {                   
                var result = JSON.parse(chunk);
                 console.log(JSON.stringify(result));
                 callback(null, result)
                 });
        }).on('error', function(e) {
              callback(err);
            console.log("Got error: " + e.message);
          })  
        },
        function processData(results,callback){
            var sets = {};
              sets.videotitle = results.title;
              sets.videothumb_url= results.thumburl;
              sets.videoiframe= results.html;
              sets.videoheight= results.height;
              sets.videowidth= results.width;
              sets.videoauthor= results.author_name;
              sets.videoauthurl= results.author_url;
              callback(null,sets);                
            },
            function updateArray(resu, callback){
                 resultArr.push(resu);
                 console.log(resu);
                 callback(null);                    
         }
       ],
      function finalcall(){            
             console.log('operation completed');
             console.log(resultArr.length);
             console.log(JSON.stringify(resultArr));
             callback(null);    
         });         
  }
 async.each(data, iter, function(){                 
         console.log('array completed');
         console.log(resultArr.length);
         return resultArr;            
         console.log(JSON.stringify(resultArr));                    
    });       
 }
 // caller function 
  exports.uploadPaletteVideo = function(req,res){  
       Palette.findById(req.body.palid, function(err, videodoc){
              if(err){
               util.log('palette with id not found video '+err);
            }else{               
 var mar = [{'src':'http://www.youtube.com/watch?v=k98k47X_Ugo'},    {'src':'https://www.youtube.com/watch?v=6OLsmfxalPw'}];
            console.log('the palette' + getWaterfall(mar));               
            //console.log('From PaletteVideo'+ JSON.stringify(youtube));              
           }                            
    }) 
 res.send('success');
}      

我的代码无法将数组返回给调用函数,但是我能够将此数组传递给另一个函数并且正常工作。请帮帮我..

0 个答案:

没有答案