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

201
Views
Implicit calling then() method with await keyword

When i run this code:

    class Thenable {
  constructor(num) {
    this.num = num;
  }
  then(resolve, reject) {
    console.log(resolve);
    
    setTimeout(() => resolve(this.num * 4), 1000); 
  }
};

async function f() {

  let result = await new Thenable(1); //then() method is called automatically
  console.log(result); 
}

f();

I noticed that then() method is called without explicit call.

I was surprised, because i thought when javascript meets await then it just automatically returns resolved value inside executor function like in this code:

async function f() {

  let promise = new Promise((resolve, reject) => {
      this.num  = 1;
     console.log('this.num = ' + this.num); 
    setTimeout(() => resolve(this.num * 4), 1000)
  });

  let result = await promise; //just assigns resolved value inside executor function into result variable

  console.log(result+'!'); 

}

f();

Since Promise is Thenable then questions are:

-How it works under hood in case when Promise is used with await?

-Does javascript calls then() method of Promise automatically when it meets await?

-Or does javascript distinguish two cases and processes differently in case with Tenable and Promise?

about 4 years ago · Juan Pablo Isaza
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!