Created
January 23, 2021 18:50
-
-
Save bbachi/3a2ebec4059a6c702ea8c06854efc64b to your computer and use it in GitHub Desktop.
Asynchronous
This file contains 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
var rp = require('request-promise'); | |
function printUsers(users) { | |
console.log('Users Length:::', users.length); | |
console.log(users); | |
} | |
function printEmails(emails) { | |
console.log('Emails Length:::', emails.length); | |
console.log(emails); | |
} | |
function printAddresses(addresses) { | |
const addrs = JSON.parse(addresses); | |
console.log('Addresses Length:::', addrs.length); | |
console.log(addrs); | |
} | |
function getLastName(data) { | |
printUsers(JSON.parse(data)); | |
users = JSON.parse(data); | |
var lastName = ''; | |
users.forEach(usr => { | |
if(usr.lastName === 'last3') { | |
lastName = usr.lastName; | |
}}) | |
return lastName; | |
} | |
function getEmailAddress(data) { | |
printEmails(JSON.parse(data)); | |
const email = JSON.parse(data)[0]; | |
return email.email; | |
} | |
async function seriesAsyncAwait() { | |
try { | |
const users = await rp('http://localhost:3070/users'); | |
const lastName = getLastName(users); | |
const emails = await rp(`http://localhost:3070/user/email/${lastName}`); | |
const email = getEmailAddress(emails); | |
const addr = await rp(`http://localhost:3070/user/address/${email}`); | |
printAddresses(addr); | |
} catch(err) { | |
console.log(err); | |
} | |
} | |
seriesAsyncAwait(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment