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:

  1. 'use strict'
  2. const { promisify } = require('util')
  3.  
  4. const print = (err, contents) => {
  5. if (err) console.error(err)
  6. else console.log(contents)
  7. }
  8.  
  9. const opA = (cb) => {
  10. setTimeout(() => {
  11. cb(null, 'A')
  12. }, 500)
  13. }
  14.  
  15. const opB = (cb) => {
  16. setTimeout(() => {
  17. cb(null, 'B')
  18. }, 250)
  19. }
  20.  
  21. const opC = (cb) => {
  22. setTimeout(() => {
  23. cb(null, 'C')
  24. }, 125)
  25. }
  26.  
  27. // solution 1
  28. const printHelper = (contents) => print(null, contents)
  29.  
  30. const pOpA = promisify(opA)
  31. const pOpB = promisify(opB)
  32. const pOpC = promisify(opC)
  33. pOpA()
  34. .then(printHelper)
  35. .then(() => pOpB())
  36. .then(printHelper)
  37. .then(() => pOpC())
  38. .then(printHelper)

Solution 2:

  1. 'use strict'
  2. const { promisify } = require('util')
  3.  
  4. const print = (err, contents) => {
  5. if (err) console.error(err)
  6. else console.log(contents)
  7. }
  8.  
  9. const opA = (cb) => {
  10. setTimeout(() => {
  11. cb(null, 'A')
  12. }, 500)
  13. }
  14.  
  15. const opB = (cb) => {
  16. setTimeout(() => {
  17. cb(null, 'B')
  18. }, 250)
  19. }
  20.  
  21. const opC = (cb) => {
  22. setTimeout(() => {
  23. cb(null, 'C')
  24. }, 125)
  25. }
  26.  
  27. // solution 2
  28.  
  29. const pOpA = promisify(opA)
  30. const pOpB = promisify(opB)
  31. const pOpC = promisify(opC)
  32.  
  33. const operate = async () => {
  34. print(null, await pOpA())
  35. print(null, await pOpB())
  36. print(null, await pOpC())
  37. }
  38.  
  39. operate()

Welcome!

It looks like you're new here. Sign in or register to get started.
Sign In

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)

  • 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:

    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

    1. const { promisify } = require("util");
    2.  
    3. const print = (err, contents) => {
    4. if (err) console.error(err);
    5. else console.log(contents);
    6. };
    7.  
    8. const opA = (cb) => {
    9. setTimeout(() => {
    10. cb(null, "A");
    11. }, 500);
    12. };
    13.  
    14. const opB = (cb) => {
    15. setTimeout(() => {
    16. cb(null, "B");
    17. }, 250);
    18. };
    19.  
    20. const opC = (cb) => {
    21. setTimeout(() => {
    22. cb(null, "C");
    23. }, 125);
    24. };
    25.  
    26. const opAProm = promisify(opA);
    27. const opBProm = promisify(opB);
    28. const opCProm = promisify(opC);
    29.  
    30. opAProm()
    31. .then(print)
    32. .then(() => opBProm())
    33. .then(print)
    34. .then(() => opCProm())
    35. .then(print);
  • Posts: 58

    @nosvalds said:

    You actually don't need to abstract the print function

    1. const { promisify } = require("util");
    2.  
    3. const print = (err, contents) => {
    4. if (err) console.error(err);
    5. else console.log(contents);
    6. };
    7.  
    8. const opA = (cb) => {
    9. setTimeout(() => {
    10. cb(null, "A");
    11. }, 500);
    12. };
    13.  
    14. const opB = (cb) => {
    15. setTimeout(() => {
    16. cb(null, "B");
    17. }, 250);
    18. };
    19.  
    20. const opC = (cb) => {
    21. setTimeout(() => {
    22. cb(null, "C");
    23. }, 125);
    24. };
    25.  
    26. const opAProm = promisify(opA);
    27. const opBProm = promisify(opB);
    28. const opCProm = promisify(opC);
    29.  
    30. opAProm()
    31. .then(print)
    32. .then(() => opBProm())
    33. .then(print)
    34. .then(() => opCProm())
    35. .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
  • Posts: 58

    @davidmarkclements Thank you for your advices.

Welcome!

It looks like you're new here. Sign in or register to get started.
Sign In

Welcome!

It looks like you're new here. Sign in or register to get started.
Sign In

Categories

Upcoming Training