Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

When does the promise constructor return a promise object?


Asked by Joanna Newton on Dec 10, 2021 FAQ



When called via new, the Promise constructor returns a promise object. The promise object will become "resolved" when either of the functions resolutionFunc or rejectionFunc are invoked.
Furthermore,
The promise constructor takes one argument, a callback with two parameters, resolve and reject. Do something within the callback, perhaps async, then call resolve if everything worked, otherwise call reject. Like throw in plain old JavaScript, it's customary, but not required, to reject with an Error object.
Indeed, The new Promise() constructor returns a promise object. As the executor function needs to handle async operations, the returned promise object should be capable of informing when the execution has been started, completed (resolved) or retuned with error (rejected).
In addition,
executor. A function to be executed by the constructor, during the process of constructing the new Promise object. The executor is custom code that ties an outcome to a promise. You, the programmer, write the executor. The signature of this function is expected to be: function(resolutionFunc, rejectionFunc){ }
Thereof,
The promise object will become "resolved" when either of the functions resolutionFunc or rejectionFunc are invoked. Note that if you call resolutionFunc or rejectionFunc and pass another Promise object as an argument, you can say that it is "resolved", but still cannot be said to be "settled".