DEV Community

PUSHAN VERMA
PUSHAN VERMA

Posted on

1

Promises in JavaScript

const fs =require('fs');

console.log("Before");

// ๐Ÿ“Œto read file normally through callbacks
// fs.readFile("f1.txt",function(err,data){
// if(err)
// {
// console.log(error);
// }
// else{
// console.log("file data ->"+data);
// }
// })

// ๐Ÿ‘‰ans -> Before
// file data ->this is file 1

//๐Ÿ“Œ reading file by PROMISE
// (this is the first stage , where the promise shows pending )
let promise =fs.promises.readFile('f1.txt')
// (here we have used promise present inside the fs module)
console.log(promise);
// ๐Ÿ‘‰(if we replace the file with f3.txt , then it would not run because it will not run )
// ans->
// Before
// Promise { }
// After
// (after pending stage , we have two stages -either to get fulfilled or to get rejected)
// fulfill - is done by .then
// reject - is done by .catch
// just like try and catch

promise.then(function(data){
console.log("File data->"+data);
})

promise.catch(function(err){
console.log(err);
})
//๐Ÿ‘‰ ans -> Before
// Promise { }
// After
// File data->this is file 1

console.log('After');

HOW TO CONSTRUCT A PROMISE ?

let promise = new Promise(function(resolve,reject){
const a=2;
const b=2;
if(a===b)
{
// resolve();
resolve("yes value are equal");
//resolve calls .then function
}
else
{
// reject();

reject("no value are not equal");
//reject calls ,.catch function

}

// you cannot put .then and catch inside the promise 
Enter fullscreen mode Exit fullscreen mode

})

// ๐Ÿ“Œ1st way

// promise.then(function(){
// console.log("Equal");
// })

// promise.catch(function(){
// console.log("Not Equal");
// })

//๐Ÿ‘‰ ans ->"Equal"(because value of a & b is 2 )
//๐Ÿ‘‰ ans -> "Not Equal"(because value of a and b is differennt a=2 , b=4)

// ๐Ÿ“Œ2nd way(through CHAINING OF THEN AND CATCH)
// promise.then(function(){
// console.log("Equal");
// }).catch(function(){
// console.log("Not Equal");
// })

//๐Ÿ“Œ 3rd way(writing in resolve and reject and returning in then and catch)

promise.then(function(data){
// console.log(data);
console.log("result coming from resolve method"+data);
}).catch(function(err){
// console.log(err);
console.log("result coming from reject method"+err);
})

//๐Ÿ‘‰ans ->yes value are equal
//๐Ÿ‘‰ans ->result coming from resolve methodyes value are equal

๐Ÿ“ŒHandwritten Notes:
https://github.com/pushanverma/notes/blob/main/webd/Promises%20in%20Js.pdf

Image of Timescale

๐Ÿš€ pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applicationsโ€”without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read full post โ†’

Top comments (0)

The best way to debug slow web pages cover image

The best way to debug slow web pages

Tools like Page Speed Insights and Google Lighthouse are great for providing advice for front end performance issues. But what these tools canโ€™t do, is evaluate performance across your entire stack of distributed services and applications.

Watch video