Created
January 3, 2018 04:53
-
-
Save hellobrian/a5f70afdb480fd68efdcc06bd2ad09e2 to your computer and use it in GitHub Desktop.
async-await.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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