Skip to content

Update context from promise + async iterator stream #5241

Answered by Andarist
drewdelano asked this question in Q&A
Discussion options

You must be logged in to vote

I think this does the trick (TS playground):

// @target: esnext

import { setup, fromPromise, ActorRef, Snapshot } from "xstate"; // types: 5.19.2

async function* createAsyncIterator() {
  yield "Hello";
  await new Promise((resolve) => setTimeout(resolve, 1000));
  yield "World!";
  await new Promise((resolve) => setTimeout(resolve, 1000));
  yield "Async Iterators are cool!";
}

type ParentEvent = { type: "UPDATE_VALUE"; value: string };

setup({
  types: {} as {
    context: {
      value: string;
    };
    events: ParentEvent;
  },
  actors: {
    consumeAsyncIterator: fromPromise(
      async ({
        input: { parent },
      }: {
        input: { parent: ActorRef<Snapshot<unknown>,

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by drewdelano
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants