Last active
February 25, 2025 18:12
-
-
Save mmocny/e17e42823e8c98fe96202f2726bde8af to your computer and use it in GitHub Desktop.
AsyncContextWithPromisesDream.js
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
const val = AsyncContext.Variable(); | |
async main() { | |
console.log(val.get()); // Expect: undefined | |
await val.withValue(1); | |
console.log(val.get()); // Expect: 1 | |
await 1; | |
console.log(val.get()); // Expect: 1 | |
await foo(); // Expect: Promise created, and captures the current scope. .then() and await would restore | |
console.log(val.get()); // Expect: 1 | |
} | |
async foo() { | |
console.log(val.get()); // Expect: 1 | |
await val.withValue(2); | |
console.log(val.get()); // Expect: 2 | |
// val.withValue(3).then(async () => { | |
// console.log(val.get()); // Expect: 3 | |
// await scheduler.wait(1000); | |
// }); | |
// return Promise.resolve(); | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment