Created
February 14, 2018 17:24
-
-
Save jigewxy/783c70ae56f98c6bf8cc7120ff771e1f to your computer and use it in GitHub Desktop.
ASYNC method - async.forEachOf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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