Most Popular Tutorials
Most Popular Tutorials :-

Simply Easy Learning at Your Fingertips. Click Tutorials Menu to view More Tutorial List





what is promises in node.js?

Promises - Understanding Chaining and Error Handling

then()

When a Promise is resolved, the handler function in then() is executed. The value passed by resolve() is passed as a parameter to the handler.

File Name :

var promise = new Promise(function(resolve, reject) {
resolve("Hello Sana");
});

// callback in then() is executed when promise is resolved
promise.then(function(data) {
// Hello Sana is the output
console.log(data);
});

The return value of then() is also a Promise, which is different from the original Promise :

If the handler function in then() returns a value, the Promise returned by then() is resolved with the returned value as its value.

File Name :

var promise = new Promise(function(resolve, reject) {
resolve("Hello Sana");
});

// handler in then() returns a value
// then() returns a Promise

var promise2 = promise.then(function(data) {
return data;
});

// promise2 is resolved

promise2.then(function(data) {
// Hello Sana is the output
console.log(data);
});

If the handler function in then() throws an error, the Promise returned by then() is rejected with the returned error as its value.

File Name :

var promise = new Promise(function(resolve, reject) {
resolve("Hello Sana");
});

// handler in then() throws an error
// then() returns a Promise
var promise2 = promise.then(function(data) {
throw new Error(100);
});

// handler will never be called
promise2.then(function(data) {
console.log(data);
});

// promise2 is rejected
promise2.catch(function(data) {
// Error object is the output
console.log(data);
});

If the handler function in then() returns an already resolved promise, the Promise returned by then() is resolved with that Promise's return value.

File Name :

var promise = new Promise(function(resolve, reject) {
resolve("Hello Sana");
});

// handler in then() returns a Promise
// then() returns a different Promise
var promise2 = promise.then(function(data) {
return Promise.resolve("Hello Mahira");
});

// promise2 is resolved
promise2.then(function(data) {
// Hello mahira is the output
console.log(data);
});

File Name :


Example :-

File Name :

var promise = new Promise(function(resolve, reject) {
resolve(55);
});

promise.
then(function(data) {
console.log(data);
var p = new Promise(function(resolve, reject) {
setTimeout(function() {
resolve(75);
}, 1000);
});

return p;
}).
then(function(data) {
console.log(data);

return data + 20;
}).
then(function(data) {
console.log(data);
});


// output
55
75
95

Promise Rejections are Handled by the Next Available catch() in Line

File Name :

var promise = new Promise(function(resolve, reject) {
reject(55);
});

promise.
// skipped
then(function(data) {
console.log(data);
var p = new Promise(function(resolve, reject) {
setTimeout(function() {
resolve(75);
}, 1000);
});

return p;
}).
// skipped
then(function(data) {
console.log(data);

return data + 20;
}).
// executed
catch(function(data) {
// 55 is the output
console.log(data);
}).
// executed after catch()
then(function() {
// end is the output
console.log('end');
});

File Name :

var promise = new Promise(function(resolve, reject) {
reject(55);
});

promise.
then(function(data) {
console.log(data);
var p = new Promise(function(resolve, reject) {
setTimeout(function() {
resolve(75);
}, 1000);
});

return p;
}).
catch(function(data) {
console.log(data);
var p = new Promise(function(resolve, reject) {
setTimeout(function() {
resolve(45);
}, 1000);
});

return p;
}).
then(function(data) {
console.log(data);

throw new Error(data);
}).
then(function(data) {
console.log(data);
}).
catch(function(data) {
console.log(data);

return Promise.resolve(22);
}).
then(function(data) {
console.log(data);

return new Promise(function(resolve, reject) {
setTimeout(function() {
reject(65);
}, 1000);
});
}).
catch(function(data) {
console.log(data);
return 100;
}).
then(function(data) {
console.log(data)
});


// output
55
45
Error
22
65
100





Previous Next


Trending Tutorials




Review & Rating

0.0 / 5

0 Review

5
(0)

4
(0)

3
(0)

2
(0)

1
(0)

Write Review Here