Welcome to the Linux Foundation Forum!

LFW211 lab 8.1

I think I solved the 8.1 lab correctly but I need someone expert in the matter to check the work as the solution felt a bit weird!

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

const opA = (err,cb) => {
  setTimeout(() => {
    cb(null, 'A')
  }, 500)
}

const opB = (err,cb) => {
  setTimeout(() => {
    cb(null, 'B')
  }, 250)
}

const opC = (err,cb) => {
  setTimeout(() => {
    cb(null, 'C')
  }, 125)
}

opA(null,print)
opB(null,print)
opC(null,print)

Best Answer

  • davidmarkclements
    davidmarkclements Posts: 270
    Answer ✓

    @abdullah.yf yep this completes the exercise, nice job

    @SyuheiSaito these functions don't return anything, so shouldn't be passed to then, they also have signatures that expect err and cb functions, where as then passes the prior promise into the function. Finally, chaining promises like that means the code will execute serially, and what we want is for it to execute concurrently.

Answers

  • k0dard
    k0dard Posts: 115

    Hello,
    I'm not the expert but since the lab name is parallel execution I guess your solution is OK

  • thank you

  • I've failed 8-1

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

    const opA = (err,cb) => {
    setTimeout(() => {
    cb(null, 'A')
    }, 500)
    }

    const opB = (err,cb) => {
    setTimeout(() => {
    cb(null, 'B')
    }, 250)
    }

    const opC = (err,cb) => {
    setTimeout(() => {
    cb(null, 'C')
    }, 125)
    }

    let promise = new Promise(print)

    promise.then(opA(null,print))
    .then(opB(null,print))
    .then(opC(null,print))`

  • Imho we don't need to change the lab code like @abdullah.yf, just do that no ?

    opA(print);
    opB(print);
    opC(print);

  • @dtravailloux that doesn't match the function signatures. The functions take take two arguments (err, cb).

    The main point of this exercise is to demonstrate the non-linear execution of code in the concurrency-based JS language

  • Hi @davidmarkclements, i m a bit confused.
    I too see what @dtravailloux sees, as function signature, on parallel.js and on the lab description.

    The functions opA, opB & opC, they take just ONE parameter and this is the cb, like so:

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

    Based on the above, the correct solution should be:

    opA(print)
    opB(print)
    opC(print)

  • krave
    krave Posts: 58

    Hi @theodoros , the corect solution for labs 8.1 is the same as yours in my opinion.
    All the tasks could just start in parallel.

    For labs 8.2, opA, opB and opC are just simulations to callback-based async functions.
    These functions will do their own async jobs. In most cases we don't what exactly they do.
    But we know that if there will not be errors, they finished their jobs successfully.

    After their sucess, they will invoke the callback - cb you passed to them. And the arguments passed to the callback you provide, will be error first, then the results based on their works(those async tasks).

    So the signatures of opA, opB and opC take just ONE parameter is correct!
    What matters is the signatures of cbs you provide.

    But if you promisify opA, opB and opC, promisify will take care of the cbs for you. You don't really need think about them.

  • good answer @krave

  • @theodoros same solution with mine

  • faddah
    faddah Posts: 7
    edited January 24

    here was my solution —

    const print = (err, contents) => err ? console.error(err) : 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 run = async () => {
        Promise.all([opA(print), opB(print), opC(print)])
    }
    
    run().catch(console.error)
    
    

    that returned, in the JavaScript console —

    $ node parallel.js
    C
    B
    A
    

    ... so I believe I got this one right, yes?

  • xdxmxc
    xdxmxc Posts: 139

    @faddah no this just creates a Promise from an array with three elements containing the values undefined while also executing the callbacks and then it wraps another promise (via async function) but doesn't await the promise from Promise.all either so this:

    • creates two promises that don't do anything
    • calls the ops using just callbacks

Categories

Upcoming Training