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 use in this section I will be going over a basic example that uses the promise constructor. The basic idea of a promise is that it will return an object that represents a task that will resolve of reject over a period of time. So it is an alternative to using callbacks, and 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 rather than promises
Say for some reason I just simply want to start off with a resolve promise object, or for whatever extenuating circumstance I want a resolve promise object right now for a task that will not take much time. I could use the Promise constructor to create a promise and then just call resolve inside the body of the function that I pass the resolve constructor. However there is no need to do that as there is the Promise.resolve method that can be use for this purpose.
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.
The promise resolve and reject methods are there when I want to just simple 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.