redux-thunk-plus
TypeScript icon, indicating that this package has built-in type declarations

0.0.2 • Public • Published

redux-thunk-plus

Advanced thunk middleware for Redux

Simple thunk

const INCREMENT_COUNTER = "INCREMENT_COUNTER";

function increment() {
  return {
    type: INCREMENT_COUNTER
  };
}

function incrementAsync() {
  return dispatch => {
    setTimeout(() => {
      // Yay! Can invoke sync or async actions with `dispatch`
      dispatch(increment());
    }, 1000);
  };
}

Advanced thunk

const DATA_LOADING = "DATA_LOADING";
const DATA_LOADED = "DATA_LOADED";

function fetchData(keyword = "Hi") {
  return fetch(`https://www.google.com/search?q=${keyword}`).then(res =>
    res.text()
  );
}

function loadData1() {
  return {
    // support promise factory
    $async: fetchData,
    before: () => ({ type: DATA_LOADING }),
    after: () => ({ type: DATA_LOADED })
  };
}

function loadData2() {
  return {
    // support promise object
    $async: fetchData("Xiao"),
    before: () => ({ type: DATA_LOADING }),
    after: () => ({ type: DATA_LOADED })
  };
}

function loadData3() {
  return {
    // support multiple promises/promise factories
    $async: [fetchData("Xiao"), fetchData("Hi")],
    before: () => ({ type: DATA_LOADING }),
    after: () => ({ type: DATA_LOADED }),
    success: payloads => console.log(payloads)
  };
}

Package Sidebar

Install

npm i redux-thunk-plus

Weekly Downloads

1

Version

0.0.2

License

ISC

Unpacked Size

10.2 kB

Total Files

9

Last publish

Collaborators

  • linq2js