8. Asynchronous Programming
Activity 11: Using Callbacks to Receive Results
Solution:
- Create a 
calculatefunction that takesidand acallbackas an argument:function calculate(id, callback) { } - We will first call 
getUsersto get all of the users. This will give us the address we need:function calculate(id, callback) { clientApi.getUsers((error, result) => { if (error) { return callback(error); } const currentUser = result.users.find((user) => user.id === id); if (!currentUser) { return callback(new Error('user not found')); } }); Â Â }Here, we get all of the users, then we apply the
findmethod to theuserto find the user we want from the list. If that user does not exist, we call thecallbackfunction with theUser not founderror. - Call 
getUsageto get the user's usage:clientApi.getUsage(id, (error, usage) => { if (error) { return callback(error); } Â Â });Then, we need to put the call to
getUsageinside the callback of...