redux saga - returning promises

node v4.9.1
version: 1.0.0
endpointsharetweet
From the docs... > call(fn, ...args) > > Creates an Effect description that instructs the middleware to call the function fn with args as arguments. > fn: Function - A Generator function, or normal function which returns a Promise as result > args: Array<any> - An array of values to be passed as arguments to fn > > fn can be either a normal or a Generator function. > The middleware invokes the function and examines its result. > If the result is a Promise, the middleware will suspend the Generator until the Promise is resolved, > in which case the Generator is resumed with the resolved value. or until the Promise is rejected, > in which case an error is thrown inside the Generator.
const redux = require('redux'); const saga = require('redux-saga'); const createSagaMiddleware = saga.default; const call = saga.effects.call; const sagaMiddleware = createSagaMiddleware(); const store = redux.createStore(() => {}, redux.applyMiddleware(sagaMiddleware)); const promiseFn = (flag) => { if (flag) return new Promise(resolve => resolve()); else return new Promise((r, reject) => reject()); } function* thisSagaShouldSucceed() { try { yield call(promiseFn, true); console.log('thisSagaShouldSucceed: success!'); } catch (error) { console.log('thisSagaShouldSucceed: fail!'); } } function* thisSagaShouldFail() { try { yield call(promiseFn, false); console.log('thisSagaShouldFail: success!'); } catch (error) { console.log('thisSagaShouldFail: fail!'); } } sagaMiddleware.run(thisSagaShouldSucceed); sagaMiddleware.run(thisSagaShouldFail);
Loading…

no comments

    sign in to comment