Skip to content

Instantly share code, notes, and snippets.

@HerbertLim
Last active August 22, 2018 07:57
Show Gist options
  • Save HerbertLim/48e6a77474bc06c466fede89a30b4ce9 to your computer and use it in GitHub Desktop.
Save HerbertLim/48e6a77474bc06c466fede89a30b4ce9 to your computer and use it in GitHub Desktop.
In React Native Android, axios.get should use CancelToken for timeout.
export function myActionCreator() {
return async dispatch => {
const CancelToken = axios.CancelToken
const source = CancelToken.source()
try {
let req = null;
setTimeout(() => {
if (req == null) {
console.log(`Request Timeout with req == null.`)
source.cancel()
}
}, 4000)
req = await axios.get(getConfApi(), {cancelToken: source.token});
dispatch ({
type: ACTION_TYPE,
payload: req.data,
})
} catch (e) {
dispatch ({
type: NETWORK_ERROR,
notice: notice_network_error,
})
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment