Welcome to the Linux Foundation Forum!

Lab 8.2 with async/await

Hi, I tried lab 8.2 with callbacks and promises and it worked fine, but when I tried using async/await for the same effect I found that this code would work:

  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. const opAp = promisify(opA)
  28. const opBp = promisify(opB)
  29. const opCp = promisify(opC)
  30.  
  31.  
  32. async function run() {
  33. try {
  34. print(null, await opAp())
  35. print(null, await opBp())
  36. print(null, await opCp())
  37. } catch (err) {
  38. console.log(err)
  39. }
  40. }
  41.  
  42. run()

but this would only print 'A', and I don't understand why

  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. const opAp = promisify(opA)
  28. const opBp = promisify(opB)
  29. const opCp = promisify(opC)
  30.  
  31.  
  32. async function run() {
  33. try {
  34. await opAp(print)
  35. await opBp(print)
  36. await opCp(print)
  37. } catch (err) {
  38. console.log(err)
  39. }
  40. }
  41.  
  42. run()

Comments

  • Posts: 160

    When you pass an argument (print in this case) into a promisified function it offsets the callback that it calls internally. The confusion here is you think you have to pass the promisified function the callback, you don't - that happens internally in the promise returned from promisify. Here's your fix

    1. 'use strict'
    2. const { promisify } = require('util')
    3.  
    4. const opA = (cb) => {
    5. setTimeout(() => {
    6. cb(null, 'A')
    7. }, 500)
    8. }
    9.  
    10. const opB = (cb) => {
    11. setTimeout(() => {
    12. cb(null, 'B')
    13. }, 250)
    14. }
    15.  
    16. const opC = (cb) => {
    17. setTimeout(() => {
    18. cb(null, 'C')
    19. }, 125)
    20. }
    21.  
    22. const opAp = promisify(opA)
    23. const opBp = promisify(opB)
    24. const opCp = promisify(opC)
    25.  
    26.  
    27. async function run() {
    28. try {
    29. console.log(await opAp())
    30. console.log(await opBp())
    31. console.log(await opCp())
    32. } catch (err) {
    33. console.log(err)
    34. }
    35. }
    36.  
    37. run()
    38.  

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