Created
July 12, 2016 02:13
-
-
Save hiyangguo/547b607cc6ac0aa2b8ebab4c9409af31 to your computer and use it in GitHub Desktop.
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
```javascript | |
var tasks = [1, 2, 3, 4, 5]; | |
tasks = tasks.map(function(i) { | |
return function(cb) { | |
setTimeout(function() { | |
console.log(i); | |
cb(); | |
}, Math.random() * 500 | 0); | |
} | |
}); | |
/** | |
* 方法一 | |
* @param tasks | |
* @param cb | |
*/ | |
function seq(tasks, cb) { | |
var i = 0; | |
runTask(); | |
function runTask() { | |
var task = tasks[i]; | |
task(function() { | |
i++; | |
if (i !== tasks.length) { | |
runTask(); | |
} else { | |
cb(); | |
} | |
}); | |
} | |
} | |
seq(tasks, function() { | |
console.log('all Done'); | |
}); | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment