Skip to content

Instantly share code, notes, and snippets.

@jigewxy
Created February 14, 2018 17:24
Show Gist options
  • Save jigewxy/783c70ae56f98c6bf8cc7120ff771e1f to your computer and use it in GitHub Desktop.
Save jigewxy/783c70ae56f98c6bf8cc7120ff771e1f to your computer and use it in GitHub Desktop.
ASYNC method - async.forEachOf
const async = require('async');
var arr =[5,4,3,2,1];
async.forEachOf(arr, (index,i, callback)=>{
setTimeout(()=>{console.log(`Wait for ${index} seconds`); callback(null);}, index*1000);
//这里的关键在于callback必须是在循环的异步方法里面,否则是无法起作用的。
}, function(err){
if(err)
console.log(err.message);
else
console.log("async loop ended");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment