Some Details Concerning Promises
You will now dig into the details of what promises are and how they are used in general, not necessarily in the context of service calls.
The constructor of a promise looks like this:
new Promise(function(resolve, reject) { });
You would pass in an executor function that takes two arguments: resolve
and (optionally) reject
. When the promise is instantiated, this function is executed immediately. Your implementation of the executor function would typically initiate some asynchronous operation. Once the return value is available, it should then call the passed-in resolve
function or reject
if there is an error or other invalid condition. If an error is thrown in the executor function, it also causes the promise to be rejected (even if reject
is not called explicitly).
Put into pseudo-code, this is similar to the following:
const promise = new Promise((resolve, reject) => { Â Â Â Â // do something asynchronous, which eventually...