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

Пример с Server Sent Events и асинхронным генератором #55

Open
monochromer opened this issue Feb 2, 2025 · 0 comments

Comments

@monochromer
Copy link
Owner

Какие могут проблемы в таком примере кода:

async function* eventStream(url) {
  const eventSource = new EventSource(url);

  while (true) {
    const event = await new Promise((resolve, reject) => {
      eventSource.onmessage = resolve;
      eventSource.onerror = reject;
    });

    if (event.type === 'error') {
      eventSource.close();
      throw event;
    }

    yield event.data;
  }
}

const url = "/live-feed";
for await (const message of eventStream(url)) {
  console.log("message:", message);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant