#invoke actors (fromObservable, fromPromise)

1 messages · Page 1 of 1 (latest)

void rune
#

Hi guys, I am trying to find out how to catch the error by using fromObservable. In following example my getTariffById just throw an error, but it seems like the machine come to the done state anyway. It seems obvious that the fromPromise is the best candidate here to use instead of fromObservable, but I cannot use it with Temporal Workflow because fromObservable use AbortController under the hood, which is not defined in Temporal Workflow. Can we achive same result and error handling with fromObservable in this machine?

      types: {
        context: {},
      },
      actors: {
        fetchTariff: fromObservable(() =>
          from(getTariffById(tariffId)).pipe(
            map((response) => {
              // Process the response
              console.log("Data received:", response);
              return response;
            }),
            catchError((error) => {
              // Handle errors
              console.error("Error:", error);
              return of({ error: true, message: error.message });
            })
          )
        ),
      },
    }).createMachine({
      context: {
        user: null,
        tariff: null,
      },
      id: "CreateCheckoutSession",
      initial: "fetching",
      states: {
        fetching: {
          invoke: {
            input: {},
            onDone: {
              target: "done",
            },
            onError: {
              target: "error",
            },
            src: "fetchTariff",
          },
        },
        done: {},
        error: {},
      },
    });

    const actor = createActor(machine);

    actor.start();
fallow tartan
#

fromObservable doesn't have an output so I don't think it can catch errors like this

https://stately.ai/docs/actors#actor-logic-capabilities

When you run a state machine, it becomes an actor: a running process that can receive events, send events and change its behavior based on the events it receives, which can cause effects outside of the actor.

#

I would use sendParent to handle the outcome paths

void rune
#

Where is any workaroud for fromPromise not to use the AbortController? In xstate x4 it was just an async function

fallow tartan
#

What's the issue you're having with AbortController exactly? I'm not familiar with Temporal Workflow.

void rune
#

The code run on a specific server, which do not have access to some node utils. If I run the code with fromPromise, I will get an error AbortController is not defined

fallow tartan
#

is there a reason you can't polyfill it then?