question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Execution order with async functions/then isn' the same with the origin code

See original GitHub issue

Bug Report

Current Behavior

Compiled code’s execution order with async functions isn’ the same with the origin code. More detail below.

Origin: image

Compiled: image

Input Code

async function async1() {
  const p = async2()
  p.then(() => console.log('async2 1st then'))
  await p
  console.log('async2 after await')
}

async function async2() {
  console.log('async2')
}

async1()
new Promise(resolve => {
  console.log('2rd promise handler')
  resolve()
}).then(() => {
  console.log('2rd promise 1st then')
})

Expected behavior/code

The above code should behave exactly the same, as in Chrome 71+: image

Environment

  • Babel version(s): 6.26.0
  • Node/npm version: Node 11
  • Chrome: 71

Additional context/Screenshots

The behavior was inconsistent in older versions of node and chrome, but now they seem to be aligned.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
jridgewellcommented, Mar 7, 2019

Because the spec removed the extra tick when awaiting native promises, Babel’s behavior is the correct behavior. Fixed for free!

1reaction
zloirockcommented, Mar 7, 2019

I think that makes sense to add a test for this behavior to compat-table.

Read more comments on GitHub >

github_iconTop Results From Across the Web

async function - JavaScript - MDN Web Docs - Mozilla
An async function will return a different reference, whereas Promise.resolve returns the same reference if the given value is a promise. It can ......
Read more >
Why does this async function execute before the equivalent ...
I believe [the code] shows two equivalent ways to achieve the same functionality: first by chaining promises and second with the syntactic sugar ......
Read more >
The Execution Order of Asynchronous Functions in the Event ...
This question is actually examining the execution order of the code. To be precise, it is examining the difference between asynchronous ...
Read more >
Asynchronous programming: futures, async, await - Dart
Execution flow with async and await​​ An async function runs synchronously until the first await keyword. This means that within an async function...
Read more >
Understanding async/await execution order : r/csharp - Reddit
However, when running this code (on replit), I'm sometimes seeing surprising results. For example, sometimes one of the list items is logged ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found