NodeJs Tutorials
- NodeJs
- Install NodeJs
- Why use NodeJs
- NodeJs Process Model
- create First Application
- Run NodeJs Application
- Node.js Console
- Node.Js Modules
- URL Modules
- node.js Callback
- Node.js Events
- Upload Files
- Upload single & Multiple Files
- NodeJs File System
- NodeJs Email
- Debugging NodeJs
- .ENV
- NodeJs Mysql
- Helpers
- encription and decription in nodeJs
- Query string
- Date & Time
- Express Js
- Template Engine with Express
- MVC Pattern in Node.Js
- list of NPM Module
- Middleware
- Body Parser
- Render
- Nodemon module
- Morgan module
- Flash Message in ExpressJs
- Session
- Session store in database
- Cookies
- Helmet
- Multer
- Router: How To Use Routing In Node.Js
- App.Js
- express.json() and express.urlencoded()
- REST APIs in NodeJs
- Gloabal Objects
- Submit Form Data
- How to get Post Data in Node.js
- How to Get URL Parameters in Node.js
- How to create Node Project
- How to Insert Form Data Into the MySql Table Using Node.js
- How to fetch Data from MySQL database table using Node.js
- CRUD Example
- Await and Async
- Promises
- Login Example
- Password Encription
- How to validate Form (Form Validation) in Node.Js?
- Registration & Login form usingn Node.Js & MySql?
- Forgot & Reset Password
- File Upload in Node.Js with ExpressJs
- Resize Image Before Upload using Multer Sharp
- Upload multiple file using node.js with multer module
- Upload file using node.js with multer module
- Client IP Address
- REST API Downloading File in NodeJs
- Export and Download CSV From MySQL Database
- CRUD REST API
- CRUD REST API Example 2
- Enable HTTPS using Node
- How to send EMAIL using NodeJs?
- Dynamic dependent dropdown using NodeJs?
- Autocomplete Search
- Get and Send Data From Ajax Request
- Get and Post Data using Ajax
- Passport Authentication
- Node Js Data type
- Node Js Error
- Node Js Array Function
- Node Js String Function
- Puppeter Module
Promises - Understanding Chaining and Error Handling
https://usefulangle.com/post/76/javascript-promises-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
File Name :
File Name :
File Name :