1 回答
TA贡献1858条经验 获得超8个赞
改成这样就行了
var timeline = [
function() {
return new Promise(function(resolve, reject) {
setTimeout(function() {
resolve('task5<br>');
}, 1000);
});
},
function() {
return new Promise(function(resolve, reject) {
setTimeout(function() {
resolve('task6<br>');
}, 1500);
});
},
function() {
return new Promise(function(resolve, reject) {
setTimeout(function() {
resolve('task7<br>');
}, 2000);
});
},
function() {
return new Promise(function(resolve, reject) {
setTimeout(function() {
resolve('task8<br>');
}, 800);
});
}
];
// 完成后立刻输出结果,但是按照顺序输出
timeline.map(function(val) {
return val();
}).reduce(function(init, req) {
return init.then(function() {
return req;
}).then(function(result){
document.writeln(result)
})
}, Promise.resolve());
添加回答
举报