Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/frankfang/promisecomponent

Inspired by Flutter's FutureBuilder. Preview =>
https://github.com/frankfang/promisecomponent

Last synced: 17 days ago
JSON representation

Inspired by Flutter's FutureBuilder. Preview =>

Awesome Lists containing this project

README

        

# promiseComponent

Make async code easy again!

## Installation

```bash
yarn add promise-component
```

## Usage

ComponentOne.js

```javascript
import React from 'react';
import ReactDOM from 'react-dom';
import promiseComponent from 'promise-component';

const ComponentOne = promiseComponent({
promiser: () => ajax(),
render({props, status, result, run}) {
switch (status) {
case 'initial':
return (<>
search
>);
case 'pending':
return (<>
search
loading...
>);
case 'fulfilled':
return (<>
search


succeed: {JSON.stringify(result)}

>);
case 'rejected':
return (<>
search

failed: {(result).message}

>);
}
}
});

export default ComponentOne;

function ajax() {
return new Promise((resolve, reject) => {
setTimeout(() => {
if (Math.random() > 0.5) {
resolve([
{id: 1, name: 'Frank'},
{id: 2, name: 'Jack'}
]);
} else {
reject(new Error('network broken'));
}
}, 1500);
});
}

```