Skip to content

Instantly share code, notes, and snippets.

@bericp1
Created May 24, 2018 00:57
Show Gist options
  • Save bericp1/ce32854a629ca048d9a6768de9ef5d5d to your computer and use it in GitHub Desktop.
Save bericp1/ce32854a629ca048d9a6768de9ef5d5d to your computer and use it in GitHub Desktop.
let x = 0;
async function test() {
x += await 2;
console.log(x);
}
test();
x += 1;
console.log(x);
let x = 0;
async function test() {
x = x + await 2;
console.log(x);
}
test();
x += 1;
console.log(x);
let x = 0;
function test() {
Promise.resolve(2).then((i) => {
x += i;
})
console.log(x);
}
test();
x += 1;
console.log(x);
let x = 0;
function test() {
Promise.resolve(2).then((i) => {
x += i;
console.log(x);
})
}
test();
x += 1;
console.log(x);
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);
});
➜ 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment