Skip to content

Instantly share code, notes, and snippets.

@dantenovski
Last active August 19, 2017 23:16
Show Gist options
  • Save dantenovski/bdff0ec2ea7cf1ac9a2dcae960d9c3a4 to your computer and use it in GitHub Desktop.
Save dantenovski/bdff0ec2ea7cf1ac9a2dcae960d9c3a4 to your computer and use it in GitHub Desktop.
// sagas.js
function* fetchIncrement(){
return fetch(`http://haha.com/fetch_1`)
.then(res=>res.json())
.then(data=>{
console.log(parseInt(data)); // returns string "1" and logs 1 as expected
return parseInt(data);
}).catch(err=>console.log(err))
}
export function* incrementAsync(){
const fetchResult = yield call (fetchIncrement); // fetchResult is a Promise object, not 1
yield put({type: 'INCREMENT_COUNT', payload: fetchResult}) // fetchResult needs to be 1
}
export function* watchIncrementAsync(){
yield takeEvery('INCREMENT_ASYNC', incrementAsync);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment