Skip to content

Instantly share code, notes, and snippets.

@iMichaelOwolabi
Created February 7, 2022 08:31
Show Gist options
  • Save iMichaelOwolabi/8301f213e67e17b094f3e2de09a285b7 to your computer and use it in GitHub Desktop.
Save iMichaelOwolabi/8301f213e67e17b094f3e2de09a285b7 to your computer and use it in GitHub Desktop.
Non-blocking dependent promise implementation sample
// Application logic that get employee records alongside their individual next of kin data in a non blocking format
const getAllEmployeeInformation = async () => {
// Start recording the execution time of the following code.
console.time('better await')
const allEmployees = await getAllEmployees(); // Get all employee from the DB
// Map individual employee record with their next of kin data and return the mapped data
const employeeNextOfKinPromise = allEmployees.map(employee => {
return getEmployeeNextOfKin(employee.id).then(nextOfKin => {
return { ...employee, nextOfKin };
});
})
// Execute all the accumulated promises in parallel so that they don't block eack other and improve the response time
const allEmployeeRecords = await Promise.all(employeeNextOfKinPromise);
console.log(allEmployeeRecords);
// Stop recording the execution time and log the elapsed time to the console.
console.timeEnd('better await')
return allEmployeeRecords;
}
getAllEmployeeInformation();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment