Node Js - Introduction to Promises
A Promise represents an operation that hasn't completed yet but will produce some value in the future.
A promise represents the result of asynchronous tasks that will be either resolved (success) or rejected (Fail).
Promises reduce the complexity of callback functions to handle async responses.
Let's first understand how callback works, before learning Promises.
Example of the callback function
Example of a Promise
Differences between Callback functions and Promises
1. In callback functions, A single callback function handles both success or error but In promises, there is a separation of success(resolve) and error (reject).
2. The callback functions become worse and less readable when nested inside other callback functions and lead to callback hell.
3. You cannot resolve or reject a promise more than once but you can call callback functions any number of times.
Code for your Reference:
A Promise represents an operation that hasn't completed yet but will produce some value in the future.
A promise represents the result of asynchronous tasks that will be either resolved (success) or rejected (Fail).
Promises reduce the complexity of callback functions to handle async responses.
Let's first understand how callback works, before learning Promises.
Example of the callback function
const MyCallback = function (callback) {
const a = "dummytext";
const b = "dummytext";
// Set timeout is just to simulate a wait,
// In real-time, we will be doing some async task.
setTimeout(() => {
if (a === b) {
callback(undefined, "Success");
} else {
callback("error", undefined);
}
}, 3000);
};
MyCallback(function (error, result) {
if (error) {
return console.log(error);
}
console.log(result);
});
Example of a Promise
const promise = new Promise((resolve, reject) => {
const a = "dummytext";
const b = "dummytext";
setTimeout(() => {
if (a === b) {
resolve();
} else {
reject();
}
}, 3000);
});
promise
.then(() => {
console.log("Success");
})
.catch(() => {
console.log("error");
});
Differences between Callback functions and Promises
1. In callback functions, A single callback function handles both success or error but In promises, there is a separation of success(resolve) and error (reject).
2. The callback functions become worse and less readable when nested inside other callback functions and lead to callback hell.
3. You cannot resolve or reject a promise more than once but you can call callback functions any number of times.
Insert Document to the MongoDb database using Promises
Code for your Reference:
const mongodb = require("mongodb");
const ObjectID = mongodb.ObjectID;
const MongoClient = mongodb.MongoClient;
const connectionURL = "mongodb://localhost:27017";
const dbName = "demoProject";
MongoClient.connect( connectionURL, { useUnifiedTopology: true }, (error, client) => {
if (error) {
return console.log(error);
}
const db = client.db(dbName);
db.collection("products").insertOne(
{
title:'A book'
}
).then((result)=>{
console.log("Inserted Id: "+ result.insertedId)
}).catch((error)=>{
console.log("Error in document insertion")
})
}
);
0 Comments