Welcome to the Linux Foundation Forum!

Promisify based solution for lab 8.2 .

I have tried a promisify based solution to lab 8.2 .
Share with you.
Any additional advice is welcome.

Solution 1:

'use strict'
const { promisify } = require('util')

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)
}

// solution 1
const printHelper = (contents) => print(null, contents)

const pOpA = promisify(opA)
const pOpB = promisify(opB)
const pOpC = promisify(opC)
pOpA()
  .then(printHelper)
  .then(() => pOpB())
  .then(printHelper)
  .then(() => pOpC())
  .then(printHelper)

Solution 2:

'use strict'
const { promisify } = require('util')

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)
}

// solution 2

const pOpA = promisify(opA)
const pOpB = promisify(opB)
const pOpC = promisify(opC)

const operate = async () => {
  print(null, await pOpA())
  print(null, await pOpB())
  print(null, await pOpC())
}

operate()

Comments

  • Hi,

    I followed the .then approach according to the training Promises (Cont.) chapter where we want to execute serial each function.

    const contentA = promisify(opA)
    const contentB = promisify(opB)
    const contentC = promisify(opC)

    contentA()
    .then((contents) => {
    print(contents)
    return contentB()
    })
    .then((contents) => {
    print (contents)
    return contentC()
    })
    .then(print)

  • krave
    krave Posts: 58

    @ghortat said:
    Hi,

    I followed the .then approach according to the training Promises (Cont.) chapter where we want to execute serial each function.

    const contentA = promisify(opA)
    const contentB = promisify(opB)
    const contentC = promisify(opC)

    contentA()
    .then((contents) => {
    print(contents)
    return contentB()
    })
    .then((contents) => {
    print (contents)
    return contentC()
    })
    .then(print)

    That looks similar as mine. I abstracted the print function into a printHelper to make the code compact.

  • @krave said:

    @ghortat said:
    Hi,

    I followed the .then approach according to the training Promises (Cont.) chapter where we want to execute serial each function.

    const contentA = promisify(opA)
    const contentB = promisify(opB)
    const contentC = promisify(opC)

    contentA()
    .then((contents) => {
    print(contents)
    return contentB()
    })
    .then((contents) => {
    print (contents)
    return contentC()
    })
    .then(print)

    That looks similar as mine. I abstracted the print function into a printHelper to make the code compact.

    You actually don't need to abstract the print function

    const { promisify } = require("util");
    
    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 opAProm = promisify(opA);
    const opBProm = promisify(opB);
    const opCProm = promisify(opC);
    
    opAProm()
      .then(print)
      .then(() => opBProm())
      .then(print)
      .then(() => opCProm())
      .then(print);
    
  • krave
    krave Posts: 58

    @nosvalds said:

    @krave said:

    @ghortat said:
    Hi,

    I followed the .then approach according to the training Promises (Cont.) chapter where we want to execute serial each function.

    const contentA = promisify(opA)
    const contentB = promisify(opB)
    const contentC = promisify(opC)

    contentA()
    .then((contents) => {
    print(contents)
    return contentB()
    })
    .then((contents) => {
    print (contents)
    return contentC()
    })
    .then(print)

    That looks similar as mine. I abstracted the print function into a printHelper to make the code compact.

    You actually don't need to abstract the print function

    const { promisify } = require("util");
    
    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 opAProm = promisify(opA);
    const opBProm = promisify(opB);
    const opCProm = promisify(opC);
    
    opAProm()
      .then(print)
      .then(() => opBProm())
      .then(print)
      .then(() => opCProm())
      .then(print);
    

    Hi, nosvalds

    If you look close at the code and set a debugger in the print function. Run the code in debug mode and you will notice that the actual method which log out A, B, C is console.error.

    I believe the intention of this function is for whatever error occurs. Not the normal workflow. So I adjusted the handler.

  • Prefer async/await to .then and .catch because

    • it allows for async stack traces (stack traces over multiple event loop ticks that trace the awaiting of promises in async functions)
    • if you forget the .catch handler you end up with a unhandled rejection handler
    • less code noise with async await + less chance of deep nesting
  • krave
    krave Posts: 58

    @davidmarkclements Thank you for your advices.

Categories

Upcoming Training