Example for using async/await (#11)
* Use babelrc * Use async to unnest
This commit is contained in:
parent
4130a08199
commit
f6e551c45b
3 changed files with 21 additions and 38 deletions
|
|
@ -28,7 +28,7 @@
|
|||
"scripts": {
|
||||
"check": "node_modules/.bin/eslint --ext \".js\" --ignore-pattern node_modules --quiet .",
|
||||
"start": "node node_modules/react-native/local-cli/cli.js start",
|
||||
"test": "mocha --compilers js:babel-register",
|
||||
"test": "mocha --compilers js:babel-register --require babel-polyfill",
|
||||
"postinstall": "remotedev-debugger --hostname localhost --port 5678 --injectserver"
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,21 +9,14 @@ export const DEVICE_SUCCESS = 'DEVICE_SUCCESS';
|
|||
export const DEVICE_FAILURE = 'DEVICE_FAILURE';
|
||||
|
||||
function fetchDevice() {
|
||||
return (dispatch) => {
|
||||
dispatch(requestData(DEVICE_REQUEST));
|
||||
|
||||
AsyncStorage.getItem('basic_info', (err, data) => {
|
||||
if (err) {
|
||||
dispatch(requestFailure(DEVICE_FAILURE, {msg: 'failed to load local storage'}));
|
||||
}
|
||||
|
||||
if (data && data.length > 0) {
|
||||
const json = JSON.parse(data);
|
||||
dispatch(requestSuccess(DEVICE_SUCCESS, json));
|
||||
} else {
|
||||
dispatch(requestSuccess(DEVICE_SUCCESS, {hello: 'hello'}));
|
||||
}
|
||||
});
|
||||
return async (dispatch) => {
|
||||
try {
|
||||
dispatch(requestData(DEVICE_REQUEST));
|
||||
const json = await AsyncStorage.getItem('basic_info');
|
||||
dispatch(requestSuccess(DEVICE_SUCCESS, JSON.parse(json)));
|
||||
} catch (err) {
|
||||
dispatch(requestFailure(DEVICE_FAILURE, {msg: 'failed to load local storage'}));
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
|
@ -31,4 +24,4 @@ export function loadDevice() {
|
|||
return (dispatch, getState) => { // eslint-disable-line no-unused-vars
|
||||
return dispatch(fetchDevice());
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -240,29 +240,19 @@ export default class Client {
|
|||
);
|
||||
}
|
||||
|
||||
doFetch = (url, options, onRequest, onSuccess, onFailure) => {
|
||||
doFetch = async (url, options, onRequest, onSuccess, onFailure) => {
|
||||
if (onRequest) {
|
||||
onRequest();
|
||||
}
|
||||
|
||||
return fetch(url, this.getOptions(options)).then(
|
||||
(response) => {
|
||||
return response.json().then((data) => ({data, response}));
|
||||
}).then(({data, response}) => {
|
||||
if (!response.ok) {
|
||||
return Promise.reject(data);
|
||||
}
|
||||
|
||||
return onSuccess(data, response);
|
||||
}).catch((err) => {
|
||||
// TODO errors that return non-json data get sent here
|
||||
|
||||
if (this.logToConsole) {
|
||||
console.log(err); // eslint-disable-line no-console
|
||||
}
|
||||
|
||||
onFailure(err);
|
||||
try {
|
||||
const response = await fetch(url, this.getOptions(options));
|
||||
const data = await response.json();
|
||||
return response.ok ? onSuccess(data, response) : Promise.reject(data);
|
||||
} catch (err) {
|
||||
if (this.logToConsole) {
|
||||
console.log(err); // eslint-disable-line no-console
|
||||
}
|
||||
);
|
||||
return onFailure(err);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue