Skip to content

Instantly share code, notes, and snippets.

@brianc
Last active October 6, 2015 20:48
Show Gist options
  • Save brianc/9ae6b1a07d296824816e to your computer and use it in GitHub Desktop.
Save brianc/9ae6b1a07d296824816e to your computer and use it in GitHub Desktop.
//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 })
})
}
}
//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