Promise all method for a collection of promises

When a whole bunch of tasks need to be accomplished before moving on with things, some or all of which might take a while, one way to do so is with the Promise.all method. This method will return a resolved promise object when everything that is given to it via an array as the first argument is resolved if a promise, or is something that is not a promise. In Other words the promise all method will only resolve when every element in a given array is a value, or a promise object that has been resolve, rather than rejected. The Promise all method will then only result in a rejected promise if one or more promise in the array reject. So then the array that is given to the promise all method can be a mixed collection of values, some of which can be promises, and things will not continue until all promises in the array are resolved or rejected.

So it goes without saying that the promise all method is fairly useful whenever I am in a situation in which I need to do a whole bunch of async tasks, and then continue with more to do once all of that has completed. The promise all method should be there when it comes to native Promise support, but can also be added when working with older platforms via something like bluebird.

So then lets take a look at a few examples of the promise all method in action.

Read More

Canvas get image data method and related topics

So when it comes to working with canvas there is the get image data method that can be used to get image data from the current and area in the state of a canvas matrix in the from of an ImageData) class instance that has an unit8Clamped array in a data property that is the raw data for each color channel of each pixel in an area of interest. In addition there is also the put image data method also that is the inversion of that method that can be used to put that data back into a canvas.

In addition to the canvas 2d context methods for getting and putting image data there is the ImageData constructor that caan be used to directly create an imageData instance. So then the ImageData Constructor function can be used to create an instance of image data from scratch using just javaScript code, and some other data source to create image data. This image data that is then generated purely from javaScript code can the be used with the put image data context method as an alternative from getting data from a canvas source first.

These getImageData, putImagedata, methods along with the ImageData constructor give a way to have total pixel by pixel control over the creation, and editing of images in canvas which is something that might be desired now and then with certain projects where doing so might be called for. Doing so might be expensive in terms of system resources, but if it has to happen these methods are there to help with this sort of thing. So with that said lets look at some examples of the get image data method in canvas and some related stuff to working with image data in general with canvas and javaScript while we are at it.

Read More

node util promisify method in nodejs

In versions of node before that of 8.x if I wanted to make a node js method return a promise rather than having to deal with callbacks I would have to use some kind of user land module to promisify that method, do so manually with the Promise constructor, or use a dependency that does so out of the box such as with fs-extra for example. However in versions of node 8+ there is now the util.promisify method that can be used to promisify one of these callbacks.

Read More

Fs exists method in nodejs and better alternatives

The fs exists method in the file system module of nodejs should not be used at all these days. In node 8.x it has been deprecated, and it is reasonable that it might not work at all in future versions of nodejs. So then how does one test if a file is there or not, well there are a number of ways to do that by just opening the file, and then handle the error in the event that the file is not there.In all fairness that is how it should be done anyway using the fs exists method just makes things more complicated than they need to be.

Read More