I Understand Callback Function but I cant understand promise method and async and await.why to use this three function in node js.can give explain for example code.
-
2Questions asking to find a tutorial are off-topic to SO. Please read this [on-topic](https://stackoverflow.com/help/on-topic) [ask] – Yury Tarabanko May 08 '19 at 07:09
2 Answers
Callback
Callback is a function that is passed as an argument to another function, and it is executed at the end. Like this:
function(callback){
//you do some tasks here that takes time
callback();
}
The callback is a method to deal with asynchronous code. For example, You may need to read a data form a file in your node app, and this process takes time. So, instead of blocking your code while reading, nodejs execute other tasks and then return back after the callback is executed.
Promise
The promise is also to deal with asynchronous code like callback method does but with more readable way. For example, instead of this:
example(function(){
return example1(function(){
return example2(function(){
return example3(function(){
done()
})
})
})
})
It makes it more readable like this:
example()
.then(example1)
.then(example2)
.then(example3)
.then(done)
Async function / Await
The async function is used to write asynchronous code, specifically promises. inside of this function the keyword await is used to pause the execution of a promise until it is resolved. In other words, it waits for the promise to resolve and then resume the async function. For example:
async function example(){
var data = await getData() // it waits until the promise is resolved
return data;
}

- 1
- 1

- 404
- 3
- 7
Callback Function
var fs = require('fs');
fs.readFile(fileName, 'utf8', function read(err, contents) {
console.log(contents);
});
console.log('after calling readFile');
Here function read(err, contents){} is a callback function and prints the contents when finished with reading the file. But the problem in some cases might be is "after calling readFile" gets displayed to console before reading the file. As Node Js executes statement in asynchronous mode.
Promise
var fs = require('fs');
function readMyFile(fileName)
{
return new Promise(function(resolve,reject)
{
fs.readFile(fileName, 'utf8', function read(err, contents) {
if(err)
reject(err)
else
resolve(contents)
});
}
}
var file = readMyFile(fileName).then(result=>{console.log(result);console.log('after calling readFile'); }).catch(err=>{console.log("Error Occurred",err)});
console.log(file);
Here function readMyFile(fileName) is a function that returns promise prints the contents in then block and displays an error in the catch block. But here the line console.log(file); gets executed without waiting for the file variable being defined
Async/Await
var fs = require('fs');
function readMyFile(fileName)
{
return new Promise(function(resolve,reject)
{
fs.readFile(fileName, 'utf8', function read(err, contents) {
if(err)
reject(err)
else
resolve(contents)
});
}
}
async function read()
{
var file = await readMyFile(fileName);
console.log(file);
}
Here await hold the line until file variable gets its value
- await works only with the promise
- await can only be used inside async functions

- 41
- 7