0

I am currently working with the node-redis library, which seems to be using a promise approach. Some old articles (2021) I have been following use an older callback approach.

I am curious about the difference between

client.set('Foo', 'Bar', function(err, reply) {
  console.log(reply);
  // Follow on logic using reply

});

and

let reply = await client.set('Foo', 'Bar');
// Follow on logic using reply

What reasoning is there to use promises instead now?

Caleb Renfroe
  • 183
  • 1
  • 13
  • 1
    Promises are simply a more modern coding style. – Barmar Jun 05 '23 at 20:10
  • 1
    When you have lots of callbacks, you end with heavily nested functions if you use callbacks. `async/await` allows you to write code in a style that looks synchronous, which is easier to read and write. – Barmar Jun 05 '23 at 20:11
  • 1
    `What reasoning`, Eh!! doesn't the `await` one look much nicer to you?, it becomes even more relevant when you want the result from one callback to go to another, but here your example is not really showing the benefits. Oh, and handling errors in callback is even harder, with `async / await`, `try / catch` works as expected. – Keith Jun 05 '23 at 20:12

0 Answers0