Skip to content

Instantly share code, notes, and snippets.

@herman-rogers
Last active May 2, 2018 15:51
Show Gist options
  • Save herman-rogers/881f9fa275841a56e7ef43c1e36eca87 to your computer and use it in GitHub Desktop.
Save herman-rogers/881f9fa275841a56e7ef43c1e36eca87 to your computer and use it in GitHub Desktop.
Promise Chain Example
/*
So what I would like to happen is app
is initialize in componentDidMount(),
if we are to recieve an error, have the
initializeApp() function cancel out the
entire promise chain.
Ideally, the promise child is able to either
cancel execution of the promise and navigate to the
right place by default, or hand off the navigation to
the parent.
We could have a promise chain of
Parent, child1 -> child2, and we could have several
default behaviors. Basically we control the entire
flow of the app through streams, similar to functional
programming languages I suppose.
There's already some work on it, but I have yet to investigate it.
*/
// Inside of React component
componentDidMount() {
initializeApp().then(() => {
// Handle desired navigation here
// This gets called despite errors in
// initializeApp()
});
}
// AppActions.js
export async function initializeApp() {
try {
const creds = await getKeychainCreds();
const id = AsyncStorage.getItem('USER_ID');
await verifyCredentials(creds);
await userAccountVerified(id);
} catch(e) {
if(e.type === "USER_NOT_VERIFIED") {
// Navigate to User
}
if(e.type === "CREDENTIALS") {
// Navigate to login route
}
// else do nothing
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment