Skip to content

Expension for Redux thunk middleware to support AsyncFunction and GeneratorFuntion types to enable async/await generator/yield syntax

Notifications You must be signed in to change notification settings

kutlugsahin/redux-async-thunk

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 

Repository files navigation

redux-async-thunk

Expension for thunk middleware for redux to enable async/await or generator/yield flow. No need to install thunk middleware, it is supported as well.

installation

npm install redux-async-thunk --save

usage

Apply redux-async-thunk middleware

import { createStore, applyMiddleware } from 'redux';
import asyncThunk from 'redux-async-thunk';
import reducers from './reducers/index';

const store = createStore(
  reducers,
  applyMiddleware(asyncThunk)
);

On action

function getSomeData(){
  return async function(dispatch, getState){
    let data = await fetch('http://exampleurl.com/data');
    dispatch({type: 'MY_ACTION_TYPE', data});
  }
}

Or

function getSomeData(){
  return function* (dispatch, getState){
    let data = yield fetch('http://exampleurl.com/data');
    dispatch({type: 'MY_ACTION_TYPE', data});
  }
}

Or the original thunk way

function getSomeData(){
  return function(dispatch, getState){
    fetch('http://exampleurl.com/data').then(data => {
      dispatch({type: 'MY_ACTION_TYPE', data});
    });    
  }
}

About

Expension for Redux thunk middleware to support AsyncFunction and GeneratorFuntion types to enable async/await generator/yield syntax

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published