Skip to content
This repository has been archived by the owner on Oct 20, 2019. It is now read-only.

Cute promise state manager for react and other

Notifications You must be signed in to change notification settings

org-redtea/cute-promise-state

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Cute promise state

Cute promise state manager for react and other.

Installing

$ npm i -SE @redtea/cute-promise-state

Example

import React, {Component} from 'react';
import {promiseState} from '@redtea/cute-promise-state';

class List extends Component {
  state = {
    list: promiseState([])
      .pending(true)
  };
  
  componentDidMount() {
    this.tryFetchList();
  }
  
  async tryFetchList() {
    this.setState({
      list: this.state.list
        .pending(true)
    });
    
    try {
      const list = await fetchList();
      this.setState({
        list: this.state.list.resolved(true, list)
      });
    } catch(error) {
      this.setState({
        list: this.state.list.rejected(true, error)
      });
    } 
  }
  
  render() {
    if (this.state.list.pending()) {
      return 'fetching...';
    }
    
    if (this.state.list.rejected()) {
      return 'fail fetching';
    }
    
    const list = this.state.list.result();
    
    return (
      <ul>
        {
          list.map((text, index) => (<li key={index}>{text}</li>))
        }
      </ul>
    );
  }
}