Introduction: Callback functions are used for Asynchronous events.
What are Promises?
A promise is basically an advancement of callbacks in Node. While developing an application you may encounter that you are using a lot of nested callback functions.
This is what happens due to the nesting of callback functions. Now imagine if you need to perform multiple nested operations like this. That would make your code messy and very complex. In Node.js world, this problem is called “Callback Hell”. To resolve this issue we need to get rid of the callback functions whilst nesting. This is where Promises come into the picture. A Promise in Node means an action which will either be completed or rejected. In case of completion, the promise is kept and otherwise, the promise is broken. So as the word suggests either the promise is kept or it is broken. And unlike callbacks, promises can be chained.
Callbacks to Promises
Promises notify whether the request is fulfilled or rejected. Callbacks can be registered with the .then() to handle fulfillment and rejection. The .then() can be chained to handle the fulfillment and rejection whereas .catch() can be used for handling the errors(if any).
Nested Promises: Often you will encounter situations where you need to make use of nested Promises. Nested promises begin with a .then() and in each of the .then() we have a return statement. After the return statement, .then() follows in the same manner.
Now as compared to using the callbacks, our code looks a lot cleaner than before. As the .then() can be chained together, therefore every Promise is easy to identify in the code. The .catch(err) is executed if error(s) occurs.
Creating custom Promises
You can always create your own custom Promises in Node using the new constructor.
- Nodejs | Automatic restart NodeJs server with nodemon
- ES6 | Promises
- Node.js | fs.promises.appendFile() Method
- How to use Typescript with native ES6 Promises ?
- How to operate callback based fs.appendFile() method with promises in Node.js ?
- How to operate callback-based fs.lstat() method with promises in Node.js ?
- How to operate callback-based fs.opendir() method with promises in Node.js ?
- How to operate callback-based fs.rename() method with promises in Node.js ?
- How to operate callback-based fs.truncate() method with promises in Node.js ?
- How to operate callback-based fs.readFile() method with promises in Node.js ?
- How to operate callback-based fs.readdir() method with promises in Node.js ?
- How to operate callback-based fs.mkdir() method with promises in Node.js ?
- How to operate callback based fs.writeFile() method with promises in Node.js ?
- Node.js fs.promises.link() Method
- Nodejs - Connect MongoDB with Node app using MongooseJS
- Cowsay in Nodejs using Requests library
- Nodejs | Web Crawling using Cheerio
- Introduction to NodeJS
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.