Skip to content

Instantly share code, notes, and snippets.

@mmocny
Last active February 25, 2025 18:12
Show Gist options
  • Save mmocny/e17e42823e8c98fe96202f2726bde8af to your computer and use it in GitHub Desktop.
Save mmocny/e17e42823e8c98fe96202f2726bde8af to your computer and use it in GitHub Desktop.
AsyncContextWithPromisesDream.js
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