The JavaScript Promise Object
The concept of a Promise Object was discussed when I was analysing the behaviour of asynchronous data exchange with a webserver.
In addition to this research the Fetch() API was also discussed.
According to W3C Schools:
let myPromise = new Promise(function(myResolve, myReject) {
// "Producing Code" (May take some time)
myResolve(); // when successful
myReject(); // when error
});
// "Consuming Code" (Must wait for a fulfilled Promise)
myPromise.then(
function(value) { /* code if successful */ },
function(error) { /* code if some error */ }
);
What MDN say:
The explanation on the W3C page for the Fetch API gives an example that says that Fetch is based on asyc and await. It then goes on to simplify the explanation, but I found that it didn't really help. The second example actually uses async and wait. There is a third example using "understandable" names for variables.
The .then() Method
The .then() method is part of the JavaScript Promise Object
The .then() method is Promise callback that can have two arguments, one for success and another for failure.
myPromise.then(
function(value) { /* code if successful */ },
function(error) { /* code if some error */ }
);