JavaScript Promise,所有的。然后同时执行
目前,所有的console.log遵循。然后同时执行。 我想要每个。然后等待前一个,我该怎么做?
function doIt(sentence) {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(console.log(sentence))
}, 2000)
})
}
doIt('Place order')
.then(() => setTimeout(() => { console.log('Cut the fruit') }, 2000))
.then(() => setTimeout(() => { console.log('Add water and ice') }, 2000))
.then(() => setTimeout(() => { console.log('Start the machine') }, 2000))
.then(() => setTimeout(() => { console.log('Select topping') }, 2000))
.then(() => setTimeout(() => { console.log('Serve the ice cream') }, 2000))
At the moment all the console.log following the .then, execute at the same time.
I want every .then to wait for the previous one, how can I do it?
function doIt(sentence) {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(console.log(sentence))
}, 2000)
})
}
doIt('Place order')
.then(() => setTimeout(() => { console.log('Cut the fruit') }, 2000))
.then(() => setTimeout(() => { console.log('Add water and ice') }, 2000))
.then(() => setTimeout(() => { console.log('Start the machine') }, 2000))
.then(() => setTimeout(() => { console.log('Select topping') }, 2000))
.then(() => setTimeout(() => { console.log('Serve the ice cream') }, 2000))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须建立一个前景链。
You have to create a promise chain.