Skip to content

Commit

Permalink
README: improve description and code snippet
Browse files Browse the repository at this point in the history
  • Loading branch information
harunou committed May 15, 2023
1 parent 223bd41 commit cb2d559
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,32 @@
[![codecov](https://codecov.io/github/harunou/to-flow-generator-function/branch/main/graph/badge.svg?token=1WTYGPJ8N5)](https://codecov.io/github/harunou/to-flow-generator-function)
[![npm](https://img.shields.io/npm/v/to-flow-generator-function)](https://www.npmjs.com/package/to-flow-generator-function)

Converts a promise-returning function to a generator-returning one. This is intended to allow for the usage of type-safe yield inside a MobX flow wrapper.
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`
- `fn`: The promise-returning function to be converted into a generator-returning function.

## Examples

```typescript
import { flow } from 'mobx';
import { toFlowGeneratorFunction } from 'to-flow-generator-function';

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

const fetchUserName = (id: number) => Promise.resolve('John');
const fetchUserAge = (id: number) => Promise.resolve(25);
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);
// name is type of string
// Here, `name` has the type `string`.
const age = yield* toFlowGeneratorFunction(fetchUserAge)(id);
// age is type of number
// Here, `age` has the type `number`.
return {
id,
name,
Expand All @@ -36,7 +38,7 @@ function* fetchUserData(id: number): FlowGenerator<UserData> {

const userId = 3;
const userData = await flow(fetchUserData)(userId);

// Here, `useData` has the type `UserData`.
```

More examples in [toFlowGeneratorFunction.spec.ts](./src/toFlowGeneratorFunction.spec.ts)
Expand Down

0 comments on commit cb2d559

Please sign in to comment.