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

159
Views
Why are thenables that return a promise based on the same promise as the thenable not working

A promise p1 is chained with a thenable that returns a again a chained promise p2. p2 is p1 chained with a console.log statement:

    p0 = Promise.resolve();
    p1 = p0.then(() => {
      console.log("Step1");
      p2 = p1.then(() => {
        console.log("Step2");
      });
      return p2;
    });

I would expect the code to output "Step1" and "Step2". But it only shows "Step1". Can anybody explain, why?

I know that then() is usually chained like (p0.then(...).then(...)). So this question is not about change the code so that it runs, but about the reason why it does not work.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You've created promises that depend on each other in order to resolve, so neither resolve.

When you return a Promise from inside a .then, the Promise that that .then resolves to will only fulfill when the returned Promise fulfilled. So

p1 = p0.then(() => {
  // ...
  return p2;
});

means that p1 will only resolve when:

  • first, p0 resolves
  • second, p2 resolves

But p2 only resolves after p1 resolves due to

p2 = p1.then(() => {

So neither resolve. Step1 gets logged because p0 resolves, but Step2 doesn't because neither p1 nor p2 resolve.

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!