Last active
October 6, 2015 20:48
-
-
Save brianc/9ae6b1a07d296824816e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//canceled promise resolving with no response: | |
let pendingPromise = null | |
function fetchOnce(url) { | |
if (pendingPromise) { | |
pendingPromise.cancel() | |
} | |
return http.get('/foo').then(res => { | |
pendingPromise = null | |
return res | |
}).catch(e => { | |
pendingPromise = null | |
throw e | |
}) | |
} | |
function loadData() { | |
return (dispatch) => | |
fetchOnce().then(res => { | |
if (res) dispatch({ | |
type: 'AJAX_DONE', | |
payload: { res } | |
}) | |
}).catch(e => { | |
dispatch({ type: 'AJAX_ERROR', error: true, payload: e }) | |
}) | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//canceled promise rejecting with CancellationError | |
let pendingPromise = null | |
function fetchOnce(url) { | |
if (pendingPromise) { | |
pendingPromise.cancel() | |
} | |
return http.get('/foo').then(res => { | |
pendingPromise = null | |
return res | |
}).catch(e => { | |
pendingPromise = null | |
throw e | |
}) | |
} | |
function loadData() { | |
return (dispatch) => | |
fetchOnce().then(res => { | |
//res wont be null | |
dispatch({ | |
type: 'AJAX_DONE', | |
payload: { res } | |
}) | |
}).catch(e => { | |
dispatch({ type: 'AJAX_ERROR', error: true, payload: e }) | |
}).catch(Promise.CancellationError, () => { | |
//do nothing, promise was canceled | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment