2017-08-29 84 views
0

我有一个NodeJS程序,我有一些麻烦。NodeJS制作一个for循环异步

所以我的for循环应该起作用的方式是,它应该遍历我的SQL数据库中的索引,并且在for循环的每次运行中,都应该调用另一个发出GET请求的函数。

它实际上做的是它迭代了我的所有索引,然后一次/非常快地完成每个GET请求,最终让我受限于再次进行API调用。我尝试使用setTimeout,但它不起作用,因为它只是迭代到下一个索引,并且值最终未定义。

也尝试过一个回调,但它没有改变任何东西。我试图寻找异步,但不能真正理解我应该做的事情。也只是一个说明,实际上没有任何返回。在我的其他功能,我只是更新我的SQL数据库。我只需要for循环来执行每次运行的API调用,并且只有在完成后才能继续。

function callGet() { 

    var mysql = require('mysql'); 
    var con = mysql.createConnection({ 
     host: "localhost", 
     user: "USERNAME", 
     password: "PASSWORD", 
     database: "sys" 
    }); 

    con.query("SELECT nameAlone FROM testDB", function(err, result, fields) { 

     var i; 

     for(i = 0; i < result.length; i++){ 

     var name = result[i].nameAlone; 

     var options = { 
      method: 'GET', 
      url: 'site.com', 
      qs: { 
       key: 'MYKEY', 
       page_size: 10, 
       intent: 0, 
       itemName: name 
      }, 
      headers: { 
       'postman-token': postman-token, 
       'cache-control': 'no-cache' 
      } 
     }; 
     getListing(options,result, i);     //Only being called after the for loop iterates through all of result.length 
} 
    }); 
} 

发生的事情是这样的一个例子,让我们说的for循环遍历0-2和每次运行调用打印出一个名称的函数。取而代之的

index:0, name:Chris, index:1, name:Tony, index:2, name:John 

它会打印出:

index:0, index:1, index:2, name:Chris, name:Tony, name:John 

预先感谢任何帮助。

编辑:我已经尝试了所有答案,适用于我提出的重复问题中的问题。试图混淆其中的一个答案,但不太确定它是否可行。

编辑2:所有的解决方案都没有工作。

EDIT3(解决方案):如果有人想要一个很简单的解决办法,我发现这个包,它是非常容易设置:https://github.com/nathan818fr/async-loop

我所要做的就是创建我的索引和设置asyncLoop数组如下:

asyncLoop(indices, function (item, next) 
{ 
    var name = result[item].nameAlone; 
    var options = { 
     method: 'GET', 
     url: 'site.com', 
     qs: { 
      key: 'MYKEY', 
      page_size: 10, 
      intent: 0, 
      itemName: name 
     }, 
     headers: { 
      'postman-token': postman-token, 
      'cache-control': 'no-cache' 
     } 
    }; 

    getListing(options,result, item); 
    next(); 
}, function() 
{ 
    console.log('Finished!'); 
}); 
+0

的可能的复制[异步的JavaScript循环处理(https://stackoverflow.com/questions/9772400/javascript-async-loop-processing) – Paul

+0

用一个定时器,已经尝试与实际延迟和setTimeout 0技巧。 – Tony

+0

@Paul你提出的问题是关于如何阻止长时间运行的任务,从占线和挨饿其他任务。这个问题似乎是等待循环的每次迭代完成异步操作。 – apsillers

回答

0

你可以使用我的'ploop'函数的承诺。 ploop基本上接受一个返回promise的函数,函数需要的任何参数以及决定ploop是否完成的函数。你不填第四个参数。下面是直到它找到数4使用ploop绘制的随机数的示例:

// Function that returns a promise 
var searchForNumber = function(number) { 
    return new Promise(function(resolve, reject) { 
     setTimeout(function() { 
     var min = 1; 
     var max = 10; 
     var val = Math.floor(Math.random()*(max-min+1)+min); 

     console.log('Value is: ' + val.toString());   

     return resolve(val);   
     }, 1000); 
    }); 
}; 

// fn  : function that should return a promise. 
// args : the arguments that should be passed to fn. 
// donefn : function that should check the result of the promise 
// and return true to indicate whether ploop should stop or not. 
// promise: A promise value that is used internally by ploop and should never 
// be passed in by the caller of ploop. 
var ploop = function(fn, args, donefn, promise) { 
    return (promise || Promise.resolve(true))  
     .then(function() { 
      return(fn.apply(null, args)); 
     }) 
     .then(function(result) { 
     var finished = donefn(result); 
     if(finished === true){ 
      return result; 
     } else { 
      return ploop(fn, args, donefn, promise); 
     } 
    }); 
}; 

var searchFor = 4; 

var donefn = function(result) { 
    return result == searchFor; 
}; 

console.log('Searching for: ' + searchFor); 
ploop(searchForNumber, [searchFor], donefn) 
    .then(function(val) { 
    console.log('Finally found! ' + val.toString()); 
    process.exit(0); 
    }) 
    .catch(function(err) { 
    process.exit(1); 
    }); 

一切,但ploop函数本身是示例代码。以下是演示:

http://jsbin.com/woxetos/edit?js,console

+0

我现在就试试这个谢谢,必须移动一些代码才能试试 – Tony

+0

当我设置了一切,我设法找到一个稍微不同的解决方案,所以我现在很好,但你带我到解决方案,所以谢谢你:)我完成处理我的代码后,我会给这个解决方案也试试。花了我一段时间,因为我以前没有处理过承诺。再次感谢 – Tony