to-flow-generator-function
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

ToFlowGeneratorFunction

codecov npm

The toFlowGeneratorFunction is a utility function that converts a promise-returning function into a generator-returning function. It is designed to enable the usage of type-safe yield statements inside MobX flow wrapper.

Parameters

  • fn: The promise-returning function to be converted into a generator-returning function.

Examples

import { flow } from 'mobx';
import { toFlowGeneratorFunction, type FlowGenerator } from 'to-flow-generator-function';

interface UserData {
    id: number;
    name: string;
    age: number;
}

const fetchUserName = (id: number): Promise<string> => Promise.resolve('John');
const fetchUserAge = (id: number): Promise<number> => Promise.resolve(25);

function* fetchUserData(id: number): FlowGenerator<UserData> {
    const name = yield* toFlowGeneratorFunction(fetchUserName)(id);
    // Here, `name` has the type `string`.
    const age = yield* toFlowGeneratorFunction(fetchUserAge)(id);
    // Here, `age` has the type `number`.
    return {
        id,
        name,
        age,
    };
}

const userId = 3;
const userData = await flow(fetchUserData)(userId);
// Here, `useData` has the type `UserData`.

More examples in toFlowGeneratorFunction.spec.ts

Alternative solutions

https://mobx-state-tree.js.org/API/#togeneratorfunction

Package Sidebar

Install

npm i to-flow-generator-function

Weekly Downloads

79

Version

1.0.0

License

MIT

Unpacked Size

6.66 kB

Total Files

7

Last publish

Collaborators

  • d-harunou