Welcome to the Linux Foundation Forum!

My solution to Lab 8.1

djulius Posts: 2
edited March 22 in LFW211 Class Forum


I noticed the solution for Lab 8.1 proposed in this post.

I may have read too much into the exercise requirements, but my interpretation was to implement a program that would call the functions in the requested order (opA, then opB, then opC), ensure they run in parallel and ensure that they end in the reverse order (opC, then opB, then opA).

The solution proposed in the post above leverages the fact that the waiting times of each of the functions conveniently matches the required order. I think that is too brittle and therefore I focused on implementing a solution that would work with whatever times the functions have.

This is my code. I welcome any feedback.

const print = (err, contents) => {
  if (err) console.error(err);
  else console.log(contents);

const opA = (cb) => {
  setTimeout(() => {
    cb(null, "A");
  }, 500);

const opB = (cb) => {
  setTimeout(() => {
    cb(null, "B");
  }, 250);

const opC = (cb) => {
  setTimeout(() => {
    cb(null, "C");
  }, 125);

const getPromise = (fx, ...args) =>
  new Promise((res, rej) => {
    console.log(`calling function ${fx.name}`);
    fx(...args, (err, contents) => {
      if (err) {

(async () => {
  promiseA = getPromise(opA);
  promiseB = getPromise(opB);
  promiseC = getPromise(opC);
  await print(null, await promiseC);
  await print(null, await promiseB);
  await print(null, await promiseA);

These are the logs:

calling function opA
calling function opB
calling function opC


Upcoming Training