GithubHelp home page GithubHelp logo

harunou / to-flow-generator-function Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 0.0 1.84 MB

An utility function that makes MobX flow type-safe

License: MIT License

JavaScript 50.26% Shell 2.58% TypeScript 47.15%
mobx promise typescript generators flow

to-flow-generator-function's Introduction

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, `userData` has the type `UserData`.
type GithubProject = number;
declare function fetchGithubProjectsSomehow(): Promise<GithubProject[]>;
declare function somePreprocessing(projects: GithubProject[]): GithubProject[];

class Store {
    githubProjects: GithubProject[] = [];
    state = 'pending';

    fetchProjects = flow(function* (this: Store): FlowGenerator<GithubProject[]> {
        this.githubProjects = [];
        this.state = 'pending';
        try {
            // yield instead of await.
            const projects = yield* toFlowGeneratorFunction(fetchGithubProjectsSomehow)();
            const filteredProjects = somePreprocessing(projects);
            this.state = 'done';
            this.githubProjects = filteredProjects;
        } catch (error) {
            this.state = 'error';
        }
        return this.githubProjects;
    });
}

const store = new Store();
const projects: GithubProject[] = await store.fetchProjects();

More examples in toFlowGeneratorFunction.spec.ts

Alternative solutions

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.