题目一
Promise.reject(1)
.then(value => { console.log(value); })
.then(value => { console.log(value); },
reason => {
console.log(`reason1---${reason}`);
})
.catch(reason => {
console.log(`reason2---${reason}`);
})
结果:reason1---1?
题目二?
Promise.reject(1)
.then(value => { console.log(value); })
.then(value => { console.log(value); },
reason => {
console.log(`reason1---${reason}`);
})
.then(value => { console.log(value); },
reason => {
console.log(`reason2---${reason}`);
})
.catch(reason => {
console.log(`reason3---${reason}`);
})
题目三?
使用Promise实现每隔1秒输出1,2,3
方法1
const arr = [1, 2, 3]
arr.reduce((p, x) => {
return p.then(() => {
return new Promise(r => {
setTimeout(() => {
r(console.log(x))
}, 1000);
})
})
}, Promise.resolve())
?方法2?
const arr = [1, 2, 3]
arr.reduce((p, x) => p.then(() => new Promise(r => setTimeout(() =>
r(console.log(x)), 1000))), Promise.resolve())
|