Skip to content

Instantly share code, notes, and snippets.

@hiyangguo
Created July 12, 2016 02:13
Show Gist options
  • Save hiyangguo/547b607cc6ac0aa2b8ebab4c9409af31 to your computer and use it in GitHub Desktop.
Save hiyangguo/547b607cc6ac0aa2b8ebab4c9409af31 to your computer and use it in GitHub Desktop.
```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