0

I've been chipping away at learning Javascript via Eloquent Javascript. I am currently on Chapter 11: Asynchronous Programming.

Below I tried to make a callback-based version of a promise-based function given in an example included in the book.

Does this look equivalent to you guys? Testing suggests that it MIGHT be, but there is a rabbit hole of unpredictability introduced into other functions (to simulate network behaviour) that interact with this part of the code - so results are always slightly different. Any comments/suggestions would be appreciated.

All relevant code for this chapter is in these two files: crow-tech-js and 11_async.js as available in the online Code Sandbox for this chapter.

Promise-based anon function given in book:

(nest, content, source, callback) => {
    try {
        Promise.resolve(handler(nest, content, source)).then(response => callback(null, response), failure => callback(failure));
    } catch (exception) {
        callback(exception);
    }
}

//Below is the closure for 'handler' above (don't know if that is how you use 'closure' in a sentence)

(nest, {name,neighbors}, source) => {
    let connections = nest.state.connections;
    if (JSON.stringify(connections.get(name)) == JSON.stringify(neighbors)) return;
    connections.set(name, neighbors);
    broadcastConnections(nest, name, source);
}

My equivalent callback-based anon function

(nest, { name, neighbors }, source, callback) => {
  try {
    let connections = nest.state.connections;
    if (JSON.stringify(connections.get(name)) == JSON.stringify(neighbors)) {
      callback(null);
           return;
    }
    connections.set(name, neighbors);
    broadcastConnections(nest, name, source);
    callback(null, true);
  }
  catch (e) {
    callback(e);
  }
}
PurpleMongrel
  • 112
  • 1
  • 9
  • 1
    This question may provide some guidance on the difference between promises and callbacks: https://stackoverflow.com/questions/22539815/arent-promises-just-callbacks – lux Mar 15 '21 at 19:22
  • 5
    In my humble opinion, the book is promoting a mix of promises and plain callback systems. This is not best practice. Also the example handler doesn't does not return anything (so `undefined`), so the response will be `undefined`, and makes the use of a promise useless. Others here may correct me, but I wouldn't dwell too much on these chaotic constructs. – trincot Mar 15 '21 at 19:30
  • @trincot Hey there thanks for your input! So... the function from the book was built by another function in the chapters example code (the two links I provided) - not just written as is. So maybe it has a useless element because the function that made it was supposed to be versatile, and work for other cases as well? Also, could this promise not be useful for it's error handling aspect - even if there is no return value? – PurpleMongrel Mar 15 '21 at 20:24
  • @trincot sorry got confused editing - had to delete and start over. Please see comment above. I kinda forget exactly what was included in my original (deleted) comment... But do you mean it having been created by a function for more general cases being an excuse? I dunno I guess? Would that not make sense? – PurpleMongrel Mar 15 '21 at 20:27
  • 4
    My point is that (1) mixing the old-style callback system with promises is not elegant to say the least; (2) in this concrete case it is even not useful. I can understand that if this code is generated code, that point (2) is less of an issue, but then still point (1) is relevant. The right way to use promises is to promisify the old-style callback pattern, so you really get rid of it, and only promises remain. But OK, I am not going to read that whole chapter, so I might have missed some nuances. Still the code is not elegant. – trincot Mar 15 '21 at 20:32
  • @trincot Okay good to know mixing styles isn't ideal - I'll be reading more on the subject. Thank you again – PurpleMongrel Mar 15 '21 at 20:46
  • 2
    I agree with @trincot here. But the main problem with this code is that it's actually synchronous, so there's no good reason to use either callbacks of promises here. – Bergi Mar 15 '21 at 21:56
  • @Bergi well it's a mock network. Looks like author uses setTimeout and conditionals with Math.random simulate network failures/delays. Although, wouldn't the setTimeout in the send method (that's in the linked crow-tech.js) actually be a way to "broadcastConnections" via network flooding - making sure each layer of broadcast gets queued until all branches ready for next layer - rather than letting one branch race up ahead? – PurpleMongrel Mar 16 '21 at 03:55

0 Answers0