Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AsyncLocalStorage doesn't work with async/await #1169

Open
Janpot opened this issue Aug 30, 2023 · 7 comments
Open

AsyncLocalStorage doesn't work with async/await #1169

Janpot opened this issue Aug 30, 2023 · 7 comments
Labels

Comments

@Janpot
Copy link

Janpot commented Aug 30, 2023

Description of Bug

AsyncLocalStorage doesn't work well with async/await

Steps to Reproduce

  1. Go to https://stackblitz.com/edit/stackblitz-starters-pkxqds?file=index.mjs
    import { AsyncLocalStorage } from 'node:async_hooks';
    
    const asyncLocalStorage = new AsyncLocalStorage();
    
    asyncLocalStorage.run('hello', () => {
      console.log('store before:', asyncLocalStorage.getStore());
      setTimeout(() => {
        console.log('store after:', asyncLocalStorage.getStore());
      }, 1);
    });
    
    asyncLocalStorage.run('hello', async () => {
      console.log('store async before:', asyncLocalStorage.getStore());
      await new Promise((r) => setTimeout(r, 1));
      console.log('store async after:', asyncLocalStorage.getStore());
    });
  2. Run node index.mjs
  3. Observe output
    store before: hello
    store async before: hello
    store after: hello
    store async after: undefined
    
  4. Running the same script locally produces output
    store before: hello
    store async before: hello
    store after: hello
    store async after: hello
    
    (Running under Node.js v16.20.0 on MacOS)

Expected Behavior

AsyncLocalStorage in stackblitz works with async/await.

@Nemikolh
Copy link
Member

Hi @Janpot ! Thanks for opening this issue!

You are correct! AsyncLocalStorage does not currently works in StackBlitz. This is because we don't really have an implementation for async hooks and promise hooks. We have ideas about how to do that but we want to make sure it wouldn't impact performance too much.

We'll keep this issue open and keep you posted!

@Janpot
Copy link
Author

Janpot commented Aug 30, 2023

Thank you @Nemikolh. Are there any recommended ways to detect whether my package is running under Stackblitz?

@Nemikolh
Copy link
Member

Yes! You can use @webcontainer/env. It exposes a function isWebContainer() that works both on Node and in WebContainer.

@pi0
Copy link

pi0 commented Sep 6, 2023

Related to nuxt/nuxt#23032, it would be nice that if AsyncLocalStorage mock shows a one time warning while we are waiting for AsyncContext native support in browsers so that framework users implicitly relying on this API, consider that it is an unsupported platform feature instead of silently getting and error.

@birkskyum
Copy link

Is there any update on this?

@pi0
Copy link

pi0 commented Jan 4, 2024

@birkskyum Last time I checked with the team, it needed native browser support. The best thing we might hope is to wait for AsyncContext proposal to move forward and browsers to implement it.

@birkskyum
Copy link

birkskyum commented Jan 4, 2024

In the proposal it states:

Furthermore, the async/await syntax bypasses the userland Promises and makes it impossible for existing tools like Zone.js that instruments the Promise to work with it without transpilation.

@mgechev do you have any idea about the horizon for Angular to sunset/revamp zone.js? I've struggled with it not being compatible with modern ts compile targets, so it's holding the ecosystem back in multiple dimensions, now all the way to the tc39 proposals well beyond angular.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants