Skip to content

Instantly share code, notes, and snippets.

@hellobrian
Created January 3, 2018 04:53
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 hellobrian/a5f70afdb480fd68efdcc06bd2ad09e2 to your computer and use it in GitHub Desktop.
Save hellobrian/a5f70afdb480fd68efdcc06bd2ad09e2 to your computer and use it in GitHub Desktop.
async-await.js
const axios = require('axios');
const breakfastItems = {
coffee: '☕',
eggs: '🍳',
bacon: '🥓'
};
const makeCoffee = (cb) => {
return new Promise(resolve => {
setTimeout(() => resolve(breakfastItems.coffee), 2000);
})
}
const makeEggs = (cb) => {
return new Promise(resolve => {
setTimeout(() => resolve(breakfastItems.eggs), 2500);
})
}
const makeBacon = (cb) => {
return new Promise(resolve => {
setTimeout(() => resolve(breakfastItems.bacon), 3000);
})
}
async function makeBreakfast() {
const coffee = await makeCoffee().then((response) => {
console.log('coffee is ready');
return response;
});
const eggs = await makeEggs().then((response) => {
console.log('eggs are ready');
return response;
});
const bacon = await makeBacon().then((response) => {
console.log('and the bacon is ready');
return response;
});
const brian = await axios('https://api.github.com/users/hellobrian');
console.log(`${brian.data.name}, your breakfast is ready: ${coffee} ${eggs} ${bacon}`);
}
makeBreakfast()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment