Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

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

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))
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You have to create a promise chain.

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

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!