Sunday 5 August 2018

Promises in node.js

As mentioned in my callbacks post some of the issues with callbacks can be solved with promises. Also once you understand how the core promise methods work you can use a few tricks to make your code look much nicer.

Firstly we execute our promise returning method "writeFile", this returns a promise that we can call .then() on. The function that we pass into .then() will be executed once the asynchronous code has finished executing much like a callback.

In this example we can see that data can be passed from the promise to the .then() function and then passed into another promise, but doesn't it look like we are going down the same too much indenting route in the code?

One of the cool things about promises is that they pass whatever is returned out of a .then() function out as a promise. Let's take a look at a couple of examples to make this clear.

We can see in this example that the text "TESTING" is returned out and wrapped in a promise, the second .then() function will receive this data as a parameter.

In this example the second block returns another promise already so that promise will be returned from the first .then(), allowing the second .then() to trigger after the first completes. This is a simple yet powerful mechanism that helps us to write more straight line code around promises and mix asynchronous and synchronous then blocks in a way where the intent of the flow is clear. And it should all work nicely as long as we remember to return the promises out of the .then() expressions.

One of the other things you will quickly need to do is create your own promises, you can wrap callback code in promises to make your life easier.

In this example, we have a function that creates a new promise wrapping the fs.writeFile function, in its simplest form a promise is an object that takes an expression with 2 parameters, resolve and reject. We can execute asynchronous code inside of the block and just make sure to call resolve when we are completed or reject in case of error/failure. In this case we implement a callback on fs.writeFile that calls resolve when completed.

We can also use util.promisify(), this function will take a method with a callback and return the same method wrapped in a promise. It should definitely work for all of the core node functionality and probably most other things, but be cautious with non node functions (from libraries), as they could implement a different signature and not work.

awesome old school GIF

So yeah that's pretty awesome! more on promises coming soon! and probably some async/await!

1 comment:

  1. Tioga Downs, a on line casino along the Susquehanna River on the New York facet of the Pennsylvania border, got similar approval to take away 5.3% of its machines. Harry Benson/Daily Express/Hulton Archive/Getty ImagesWhile slots have a number of the} greatest odds in a on line casino, sharp gamblers understand that the pay fee for them means very little. So whereas the machine 카지노 is theoretically paying out more cash, the odds are all the time in the house’s favor. In April, Steve, a 35-year-old filmmaker from Los Angeles, was on the Venetian in Las Vegas enjoying in} a Wheel of Fortune slot machine and feeling like a million bucks. He was taking $7 spins and after an hour of sinking $500 into the machine, Steve got a gold spin bonus and gained $700.

    ReplyDelete