Last active
February 1, 2016 08:21
-
-
Save ccbikai/ed008609912cd9056c9d to your computer and use it in GitHub Desktop.
数组转换成 Promise 的序列
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
[1,2,3].reduce(function(sequence, i) { | |
return sequence.then(function() { | |
return Promise.resolve(i); | |
}).then(function(j) { | |
console.log(j); | |
}); | |
}, Promise.resolve()); | |
// 从一个完成状态的 Promise 开始 | |
var sequence = Promise.resolve(); | |
[1,2,3].forEach(function(i) { | |
// 从 sequence 开始把操作接龙起来 | |
sequence = sequence.then(function() { | |
return Promise.resolve(i); | |
}).then(function(j) { | |
console.log(j); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment