➜ node 1.js # original
1
2
➜ node 2.js # Not using += (I know, silly)
1
2
➜ node 3.js # Ditching async and using Promise.resolve instead of await
0
1
➜ node 4.js # Same as 3 but include console.log in then
1
3
➜ node 5.js # Same as 4 but resolve result from test() call before x += 1
2
3
-
-
Save bericp1/ce32854a629ca048d9a6768de9ef5d5d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let x = 0; | |
async function test() { | |
x += await 2; | |
console.log(x); | |
} | |
test(); | |
x += 1; | |
console.log(x); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let x = 0; | |
async function test() { | |
x = x + await 2; | |
console.log(x); | |
} | |
test(); | |
x += 1; | |
console.log(x); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let x = 0; | |
function test() { | |
Promise.resolve(2).then((i) => { | |
x += i; | |
}) | |
console.log(x); | |
} | |
test(); | |
x += 1; | |
console.log(x); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let x = 0; | |
function test() { | |
Promise.resolve(2).then((i) => { | |
x += i; | |
console.log(x); | |
}) | |
} | |
test(); | |
x += 1; | |
console.log(x); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let x = 0; | |
function test() { | |
return Promise.resolve(2).then((i) => { | |
x += i; | |
console.log(x); | |
}) | |
} | |
Promise.resolve(test()).then(() => { | |
x += 1; | |
console.log(x); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment