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

Latest commit

 

History

14 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

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>
    );
  }
}

About

Cute promise state manager for react and other

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages