Skip to main content
Using Deno in production at your company? Earn free Deno merch.
Give us feedback
Module

x/xstate/src/actors/promise.ts>fromPromise

Actor-based state management & orchestration for complex app logic.
Go to Latest
function fromPromise
import { fromPromise } from "https://deno.land/x/xstate@xstate%405.8.1/src/actors/promise.ts";

An actor logic creator which returns promise logic as defined by an async process that resolves or rejects after some time.

Actors created from promise actor logic (“promise actors”) can:

  • Emit the resolved value of the promise
  • Output the resolved value of the promise

Sending events to promise actors will have no effect.

Examples

Example 1

const promiseLogic = fromPromise(async () => {
  const result = await fetch('https://example.com/...')
    .then((data) => data.json());

  return result;
});

const promiseActor = createActor(promiseLogic);
promiseActor.subscribe((snapshot) => {
  console.log(snapshot);
});
promiseActor.start();
// => {
//   output: undefined,
//   status: 'active'
//   ...
// }

// After promise resolves
// => {
//   output: { ... },
//   status: 'done',
//   ...
// }

Type Parameters

TOutput
optional
TInput = NonReducibleUnknown

Parameters

promiseCreator: (unnamed 0: { input: TInput; system: AnyActorSystem; self: PromiseActorRef<TOutput>; }) => PromiseLike<TOutput>

A function which returns a Promise, and accepts an object with the following properties:

  • input - Data that was provided to the promise actor
  • self - The parent actor of the promise actor
  • system - The actor system to which the promise actor belongs