Welcome to the Linux Foundation Forum!

Lab 8.2

2»

Comments

  • Posts: 270
    edited October 2021

    that's correct @akudrin 👍

  • Posts: 3
    edited March 2024

    I'm trying to understand the difference between calling and awaiting a promisified opA(print) vs. print(null, promisified opA) - see code below (comments in the run function):

    Can someone help me understand please?

    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 promA = promisify(opA);
    27. const promB = promisify(opB);
    28. const promC = promisify(opC);
    29.  
    30. const run = async () => {
    31. // This does not work, only prints 'A'
    32. await promA(print);
    33. await promB(print);
    34. await promC(print);
    35.  
    36. // This works as expected
    37. // print(null, await promA());
    38. // print(null, await promB());
    39. // print(null, await promC());
    40. };
    41.  
    42. run().catch(console.error);
This discussion has been closed.

Welcome!

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

Categories

Upcoming Training