Skip to content

Instantly share code, notes, and snippets.

@ccbikai
Last active February 1, 2016 08:21
Show Gist options
  • Save ccbikai/ed008609912cd9056c9d to your computer and use it in GitHub Desktop.
Save ccbikai/ed008609912cd9056c9d to your computer and use it in GitHub Desktop.
数组转换成 Promise 的序列
[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