Skip to content

Instantly share code, notes, and snippets.

@caongocthai
Last active February 8, 2023 14:02
Show Gist options
  • Save caongocthai/f1516c5f32fad7b2a13963943477562f to your computer and use it in GitHub Desktop.
Save caongocthai/f1516c5f32fad7b2a13963943477562f to your computer and use it in GitHub Desktop.
const one = () => Promise.resolve("One!")
var num = 1
async function myFunc() {
console.log('Begin function')
const res = await one()
// Lines from this point onward are suspended because of await
// If we use one().then(res => console.log(res)), [9]console.log("End function") will not be suspended and will executed before [14] console.log("After")
console.log("num = ", num)
console.log(res)
console.log("End function")
}
console.log("Before")
myFunc()
console.log("After")
num = 5
// Output:
// Before
// Begin function
// After
// num = 5
// One!
// End function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment