Skip to content

Instantly share code, notes, and snippets.

@micahriggan
Last active March 25, 2019 21:02
Show Gist options
  • Save micahriggan/a33de87bf1190ab15236875f85d0d4df to your computer and use it in GitHub Desktop.
Save micahriggan/a33de87bf1190ab15236875f85d0d4df to your computer and use it in GitHub Desktop.
Asynchronous calls which depend on the result of another asynchronous call
function fetchData(data) {
return new Promise(resolve => setTimeout(() => {
resolve(data)
}, 1000))
}
function getLoggedInUser() {
return fetchData('user1');
}
async function getDataForUser(userName) {
const profileData = await fetchData({
user1: {name: 'Micah', points: 100},
user2: {name: 'someone else', points: 200}
});
return profileData[userName];
}
async function getUserPosts(userName) {
const posts = await fetchData({
user1: ['Promises Post'],
user2: ['Streams Post']
});
return posts[userName];
}
async function userDataSerial() {
console.time('userData-serial');
const userName = await getLoggedInUser();
const userData = await getDataForUser(userName);
const userPosts = await getUserPosts(userName);
console.timeEnd('userData-serial');
}
async function userDataParallel() {
console.time('userData-parallel');
const userName = await getLoggedInUser();
const [userData, userPosts] = await Promise.all([
getDataForUser(userName),
getUserPosts(userName)
])
console.timeEnd('userData-parallel');
}
async function test() {
await userDataSerial();
await userDataParallel();
}
test();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment