Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

Promise test

/* Simple Hello World in Node.js */
console.log("Hello World");

let a =  new Promise((res, rej) => {
    res('Winning!');
});

b = a.then(
    (res) => {console.log('Resolve in B'); return new Promise((res, rej) => {
    rej('Winning!');
});},
    (rej) => {console.log('Reject in B');}
);

c = b.then(
    (res) => {console.log('Resolve in C');},
    (rej) => {console.log('Reject in C')}
);

d = c.then(
    (res) => {console.log('Resolve in D')},
    (rej) => {console.log('Reject in D')}
);

d.catch(
    (rej) => {console.log('Caught in the catch')}
);

/*
Once a Promise is fulfilled or rejected, the respective handler function (onFulfilled or onRejected) will be called asynchronously (scheduled in the current thread loop). The behavior of the handler function follows a specific set of rules. If a handler function:

 - returns a value, the promise returned by then gets resolved with the returned value as its value;
 - doesn't return anything, the promise returned by then gets resolved with an undefined value;
 - throws an error, the promise returned by then gets rejected with the thrown error as its value;
 - returns an already fulfilled promise, the promise returned by then gets fulfilled with that promise's value as its value;
 - returns an already rejected promise, the promise returned by then gets rejected with that promise's value as its value;
 - returns another pending promise object, the resolution/rejection of the promise returned by then will be subsequent to the resolution/rejection of the promise returned by the handler. Also, the value of the promise returned by then will be the same as the value of the promise returned by the handler.
*/

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.