These resolve and reject promise prototype methods will come in handy often when creating a long chain of promises. When making long promise chains I often just want to return a resolve or rejected promise inside the body of a then of catch method call in the chain. It is basically a more appropriate alternative to using the promise constructor, passing a function with a resolve argument, and then calling resolve inside the body of that function that is given to the promise constructor. This kind of solution will work, sure but why bother doing that when I have the Promise.resolve method.
So todays post will just be on the Promise.resolve, and promise.reject methods.
So if you are new to using promises or just want to review how they are used, in this section I will be going over some basic examples that use the promise constructor, as well as the Promise.resolve and Promise.reject methods. The basic idea of a promise is that it will return an object that represents a task that will resolve or reject over a period of time. The result might not come right away, and also the process of getting a result might fail. So a promise object might resolve to a requested value, and as such things can continue, or it might not at which point it will reject and the next catch function call will run. Then in the body of this catch statement call some additional action might need to be preformed such as trying again.
The typical alternative to a promise is using what is often called a callback function. Callback functions are functions that will fire at a later point, and when they do so there is often a way to check if an error happened or not in the body of the callback function. One nice thing about promises over callbacks is that the use of promises often results in a promise chain rather than the so called call back hell that happens when callbacks are used in a nested way. So the the use of promises are often regarded as a nicer way to structuring things that is easier to read and debug compared to the typical situation that is the result of using callbacks.
If you are on Github then yes the source code examples in this post are in a repository on my github account. In the for post folder there I have notes as to what I am planing out for future edits of this content. Also in that repository I have all the source code examples for all my other posts on vanilla javaScipt.
There is starting out with just some very basic examples of The promise constructor, just to get the general idea of what this is about. So for this example I am creating a new Promise object by calling the Promise constructor with the new keyword, the returned object will then be an instance of a promise to which I can call methods like then, and catch off of that will fire if the promise resolved or rejects.
When creating a Promise by way of the Promise constructor the first argument that I give to the Promise constructor function is a function that will have two arguments resolve and reject. These resolve and reject arguments will then be functions that I will call inside the body of the function that I pass to the function constructor when something happens that should resolve in the promise resolving or rejecting.
For this example I am not doing anything that will take a while such as making an http request or something to that effect. This is a very simple getting started type example after all, so for now I am just calling the resolve method inside the body of the function that I am passing to the Promise Constructor and passing a string value for the call of the resolve function. The result is then the next then function of the Promise chain being called, rather than catch.
Some times I might want to just start off with a resolve promise object, or there is a situation in which I must pass a promise and only a promise as an argument for a function. I could create one with the Promise Constructor and just resolve it like I did in the about simple example of the Promise Constructor. However there is also a static method of the Promise global for this sort of thing.
There is also the Promise reject static method that does the same thing and the Promise resolve method only it is a way to just quickly create a rejected promise rather than a resolve one. So then this will result in the next catch method in the chain being called rather than a then call.
The thing to keep in mind here with promises is that ultimately a promise object will resolve or reject. There is having something like this where there is a bunch of code that will ultimately resolve or reject when using the promise constructor. However what if you are doing something that expects a resolved or rejected promise object as a response? There should be a way to just create and return a resolved or rejected promise object right? Well yes there should be and there is, this is where the Promise resolve and reject methods come into play.
So there is no need to do create a simple Promise with the Promise constructor that just resolves right away as there is the Promise.resolve method that can be use for this purpose.
In addition to the Promise resolve method there is also the Promise reject method that will cause the next catch rather than then call to fire in a Promise chain.
One major part about Promises is to use it as a means of error handing, by way of resolving and rejecting. There is writing some kind of method that will return a promise that will resolve or reject. There is having a single call of the Promise constructor, and then calling the reject method in the method given to the constructor, only if a whole bunch of checks have passed, else there is calling the reject method, and passing a custom error object when doing so. There is also calling off of an method that will return a promise all ready and then starting a promise chain, returning calls for new promise objects by way of Promise.resolve, Promise.reject, or any other method that will return a promise as needs while making checks for various kinds of errors that will happen.
To start off this section on error handing with Promises, it might be a good idea to start off with something simple at least.
There is starting out by just creating a method that will return a promise that will resolve or reject, or calling off such a method that has been made available before hand, and just logging out the results to the standard output or the standard error. However when it comes to making some kind of real project there is actually doing something to address various kinds of errors that might happen.
For this example I have made something that is a module for what could be the start off a full application example actually. Sorry for making this one a little involved but that is what is called for in order to really drive home the point as to what errors handing is really about. It is not just logging out to the console that and error has happened, but it is also preforming specific kinds of actions for specific kinds of errors also. For example when it comes to reading a file, but it turns out that the path given is a folder rather than a file, such an error will be handled by appending a default file name to this folder and then try reading that. In the event that a file is not found that application does not just give up and fail, but writes the file and sets it up with hard coded default settings.
The main method of interest here is the read conf method, where I am staring out by reading a given URI or path if you refer to a file. This path to the file could be very much a file, but it could end up being a folder which would result in a kind of error. The file could very well be there, or it might not be which also could cause an error. It is also possible for other kinds of errors to happen for example a given file could be a file, and it could exist, but the file access permissions could be set in a way in which the script may not have the proper file access permissions to read the file which could also result in a kind of error.
Also on top off all this when it comes to what can go wrong when it comes to jus simply reading a file, there are additional things that could go wrong after that, for example say all goes well with reading the file, but then it turns out that the json will not parse because it is mail formed. There is also a situation in which all goes fine with everything including parsing, but it turns out that the json that is being read is not the json that I am looking for when it comes to the standards that I have in mind for a specific project. These things should also result in the calling of the read conf method resulting in a rejected promise. The thing about this method that is that if everything does go well then it will result in a resolved promise, with the parsed json object. So when I call this from outside of the module one of two things will happen, the next then block will run, or the next catch blog will, and it is in this catch block that I can preform various kinds of actions for these various kinds of things that can go wrong.
So then I can write a main script that I would use with this above module. In there I can of course cerate another start method in which I will be calling the read conf method of the module, and if all goes well I can just log some messages about that and end things as a resolved promise. If however one of the errors happen I can use the standard system I have made for error messages to preform specific actions for specific errors.
There is doing even more for this kind of thing, but you get the picture. There is having all kinds of different errors that can happen from calling a method that reads a file, and then preforms additional actions with that data such as parsing and validation. That is a few things that needs to happen, and for all of them one or more kinds of errors can happen.
Okay so how about another example in which I am using the Promise reject method in a promise chain. Say I want to write a script that will check if a given path is a directory and if it is fail gracefully, else it will read the contents of the file. I will first want to get the file stats of the path, and then check if the path is a directory. If the path is a director I will want to return a rejected promise that will break the promise chain, and jump to the next catch.
This might just be a silly example that will just log the error to the console in the event that the script is called on a directory rather than a file. However in a real example the script might do something more when it comes to that kind of situation. In any case this shows how the Promise reject, and resolve methods can prove to be useful in some situations. An any point in a then call I can use the Promise reject method to skip ahead to the next catch rather than then call.
The promise resolve and reject methods are there when I want to just simply have an object returned that is a resolved or rejected promise. I find myself using them some times as a way to just go about returning such an object to gain a desired result in a chain or promises.
That is it for now, but I often get around to editing my content now and then. If you have any ideas about something that should be added be sure to speak up in the comments section with it.