Skip to content

Instantly share code, notes, and snippets.

@james2406
Last active April 21, 2017 22:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save james2406/54a931ba2be5e7a8727ee2f1bcd30578 to your computer and use it in GitHub Desktop.
Save james2406/54a931ba2be5e7a8727ee2f1bcd30578 to your computer and use it in GitHub Desktop.
const asyncToken = callback => {
AsyncStorage.getItem('TOKEN').then(token => {
axios.defaults.headers = { authorization: token };
callback();
});
};
asyncToken(() => {
axios.get('http://localhost:3000/auth/testAuth').then((response) => {
alert(response.data);
}).catch(function (error) {
alert(error);
});
});
@uxxman
Copy link

uxxman commented Apr 21, 2017

you can use async and await for better code readability...

async foo() {
    const token = await AsyncStorage.getItem('TOKEN');
    axios.defaults.headers = { authorization: token };

    // Your HTTP request goes here
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment