Execution order with async functions/then isn' the same with the origin code
See original GitHub issueBug Report
Current Behavior
Compiled code’s execution order with async functions isn’ the same with the origin code. More detail below.
Origin:
Compiled:
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+:
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:
- Created 5 years ago
- Comments:7 (6 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Because the spec removed the extra tick when awaiting native promises, Babel’s behavior is the correct behavior. Fixed for free!
I think that makes sense to add a test for this behavior to
compat-table
.