Javascript promise,所有的.then同时执行

Javascript promise, all the .then execute at the same time

此时.then后面的所有console.log同时执行。 我想让每一个.then都等待上一个,怎么办?

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))

您必须创建一个承诺链。

function doIt(sentence) {
  return new Promise((resolve, reject) => {
    setTimeout(() => resolve(console.log(sentence)), 2000);
  });
}

doIt("Place order")
  .then(() => doIt("Cut the fruit"))
  .then(() => doIt("Add water and ice"))
  .then(() => doIt("Start the machine"))
  .then(() => doIt("Select topping"))
  .then(() => doIt("Serve the ice cream"));