sample1

Overview

A Promise is either in an unresolved or resolved state. We create using
new Promise, and initially, it is in the unresolved state.
The Promise object transitions to the resolved, where either its resolve or reject functions are called.

1
2
3
4
5
6

Promise objects can be in one of three states:
Pending: This is the initial state, which is neither fulfilled nor rejected.
Fulfilled: This is the final state, where it executes successfully and produces a result.
Rejected: This is the final state, where execution fails.

We generate a Promise in the following way:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
function asyncFunction(arg1, arg2) {
return new Promise((resolve, reject) => {
//perform some task or computation that's asynchronous
// for any error detected:
if (errorDetected) return reject(dataAboutError);
// When the task is finished
resolve(theResult);
});
};

/* This is the pattern that we use when promisfying an asynchronous function
that use callbacks */

function readFile(filename) {
return new Promise((resolve, reject) => {
fs.readFile(filename, (err, data) => {
if (err) reject(err);
else resolve(data);
});
});
};

asyncFunction(ar1, arg2)
.then(result) => {
// the operation succeeded
// do somethgin with the result
return newResult;
})
.catch(err => {
//an error occurred
});