promise and await

Promise


Q: what is Promises?

Ans:-
Promises are syntactical sugar that make this code slightly more readable.


1) Example of Promises
Code:-
const fs = require('fs');

// my own asynchronous function
function kiratsReadFile() {
return new Promise(function(resolve) {

fs.readFile("a.txt", "utf-8", function(err, data) {
resolve(data);
});
})
 }

// callback function to call
function onDone(data) {
console.log(data)
 }

kiratsReadFile().then(onDone);


Here's a line-by-line explanation of the code:

1. const fs = require('fs');

  • Imports the built-in  fs (file system) module, providing functions for interacting with files.

2. unction kiratsReadFile() {.....}

  • Defines a function named kiratsReadFile that handles asynchronous file reading.

3. return new Promise(function(resolve) {. . . });

  • Returns a new Promise object, enabling cleaner handling of the asynchronous operation.
4. fs.readFile("a.txt", "utf-8", function(err, data) { . . . });
  • Calls fs.readFile  to asynchronously read the contents of the file "a.txt".
    • Uses "utf-8" encoding to interpret the file's contents as text.
    • Accepts a callback function to handle the results or errors.

5. resolve(data);

  • If the file is read successfully (no errors), calls resolve to fulfill the Promise with the file's data.

6. function onDone(data) { . . . })

  • Defines a function named onDone that acts as a callback, receiving the file data.

7.  console.log(data)

  • Logs the received file data to the console.

8. kiratsReadFile().then(onDone);

  • Calls kiratsReadFile, initiating the asynchronous file reading process.
  • Chains the then method to the Promise, specifying onDone as the function to execute when the Promise resolves (file reading completes successfully).

Key Points:

  • The code demonstrates asynchronous file reading using a custom asynchronous function and Promises.
  • The fs.readFile   function handles the actual file I/O, while the Promise enables more structured handling of the asynchronous operation.
  • The then method allows for chaining actions to be performed upon successful completion of the Promise.


intimidating async function

function kiratsAsyncFunction() {
  let p = new Promise( function( resolve) {
    setTimeout( resolve, 2000)
  });
  return p;
}
const value e = kiratsAsyncFunction();
value.then( function() {
  console. log("hi there");
 })


simple function

function kiratsAsyncFunction(callback) {
  setTimeout(callback, 2000);
 }
ktratsAsyncFunction( function() {
 console. log( ("hello!");
});


Await normal syntax

function kiratsAsyncFunction() {
   let p - new Promise( function( resolve) {
    // do some async logic here
    resolve("hi there!")
   });
   return p;
 }
 function main( ) {
  ktratsAsyncFunctlon(1.then/ function( value) {
       console. log( value);
   });
 }
main();



Async await  syntax
function kiratsAsyncFunction() {
   let p = new Promise( function( resolve) {
    // do some async logic here
    resolve("hi there!")
  });
   return p;
}
async function main() {
  const value = await kiratsAsyncFunction();
  console. log(value);
}
main();


----------------------------------------
function kiratsAsyncsFunction() {
let p = new Promise (function(resolve) {
//do some async logic here
setTimeout(function() {
resolve("hi there!")
}, 3000)
});
return p;
}
async function main() {
//no callbacks, no .then syntax
let value = await ktratsAsyncFunction( )
console. log( value);
}
main();





Comments

Popular posts from this blog

CyberSecurity

VERTICAL SCALING 💋

prisma using in project idx 13.3