Full Post Page
 

Advance Js Promise Part-4

Category: JAVASCRIPT & Written by Admin On June-06-2025 11:35:05

WHAT IS PROMISE Description. A Promise is a proxy for a value not necessarily known when the promise is created. It allows you to associate handlers with an asynchronous action's eventual success value or failure reason PROMISE() METHOD 1 STAGE PENDING 2 STAGE FULFILLED--->FIXED->ANY-PLACE 3 STAGE REJECTD----->ANOTHER DAY promise() resolve() reject() | | then() catch() notes after resolve then js callback fn called then() if reject then callback fn catch called JS PROMISE SYNTAX let prom =new promise(); let prom new promise(function(){ }); let prom=new promise(function(resolve,reject){ //condition }); Examle 1 case 1 //let complete=true;// then success let complete=false;// then falied let prom =new Promise(function(resolve,reject){ if(complete){ resolve("i am done.") }else{ reject("i am failed.") } }) console.log(prom) /*OUTPUT //let complete=true;// then success Promise[[Prototype]]: Promise[[PromiseState]]: "fulfilled" [[PromiseResult]]: "i am done" let complete=false;// then falied Promise[[Prototype]]: Promise [[PromiseState]]: "rejected" [[PromiseResult]]: "i am failed." pro.html:16 Uncaught (in promise) i am failed. */ Examle 2 // case 2 function promiss(complete){ return new Promise(function(resolve,reject){ if(complete){ resolve("i am done.") }else{ reject("i am failed.") } }) } //console.log(promiss(true)) console.log(promiss(false)) // case 4 if show pending stage function promiss2(complete){ return new Promise(function(resolve,reject){ console.log("fetching data please wait pending stage") setTimeout(()=>{ if(complete){ resolve("i am successful.") }else{ reject("i am failed.") } },1000 ) }) } let onfulfilment=(result)=>{ console.log(result) } let onRejection=(error)=>{ console.log(error) } // for calling use callback fn then catch promiss2(true).then(onfulfilment) promiss2(false).catch(onRejection) or promiss2(false).then(onfulfilment).catch(onRejection) /* output fetching data please wait pending stage i am successful. i am failed */ // EXAMPLE DIVISION function promdiv(a,b){ return new Promise(function(resolve,reject){ console.log("fetching Your data please wait") var c= a/b; setTimeout(()=>{ if(a,b){ resolve(`Your answer":${c}`) }else{ reject(`I AM FAILED TO Calculate`) } },3000) }) } promdiv(26,2).then((result)=>{// pass value div console.log(result); }).catch((error)=>{ console.log(error) }) /*output fetching Your data please wait Your answer":2.5 */ //promisse with ajax step 1 copy cdn <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> step 2 json placeholder for fake data go recourse post copy url:https://jsonplaceholder.typicode.com/posts index.html <script> function promdiv(){ return new Promise(function(resolve,reject){ console.log("fetching Your data please wait") setTimeout(()=>{ $.get("https://jsonplaceholder.typicode.com/posts",function(data){ console.log(data); }).fal((err=>{ reject("failed to json load") })) },2000) }) } promdiv().then((result)=>{ console.log(result); }).catch((error)=>{ console.log(error) }) </script> topic What is Promise.all() if we have so many promise function then we have to use many catch and then function so javascrits E6 have new method promise.all promise() promise() promise() promise() | promise.all() resolve() reject() if all promise resolve if any promiser reject | | then() catch() Promise SYNTAX let p1=new Promise(function(resolve,reject){ console.log("first Promise"); resolve("prmise") }) let p2=new Promise(function(resolve,reject){ console.log("second Promise"); resolve("second ") }) let p3=new Promise(function(resolve,reject){ console.log("third Promise"); resolve("prmise") }) calling Promise.all([p1,p2,p3]).then().catch(); Example 2. let p1=new Promise(function(resolve,reject){ setTimeout(()=>{ console.log("first promise has resolved") resolve(10)// pass result value },1*1000); }) let p2=new Promise(function(resolve,reject){ setTimeout(()=>{ console.log("second promise has resolved") resolve(20) // pass result value },2*1000);//2second }) let p3=new Promise(function(resolve,reject){ setTimeout(()=>{ console.log("third promise has resolved") resolve(30)// pass result value },3*1000);//3second }) // Promise.all([p1,p2,p3]).then().catch(); Promise.all([p1,p2,p3]).then((result)=>{ for( var i in result){total+=result[i]} console.log(`Results:${result}`) console.log(`Total:${total}`) }).catch((error)=>{ console.log(`Errors:${error}`) }); /* output:first promise has resolved promiss-all.html:20 second promise has resolved promiss-all.html:26 third promise has resolved promiss-all.html:32 Results:10,20,30 Total:60 */

Comments

Share your thoughts about this post

Why Choose Us:

Experienced Instructors: Our team of experienced instructors are passionate about coding and dedicated to helping you succeed. With years of industry experience and a knack for teaching, they'll guide you every step of the way. Beginner-Friendly Content: Don't worry if you're new to coding. Our tutorials are designed with beginners in mind, starting from the fundamentals and gradually building up to more advanced topics. Updated Content: Coding is a rapidly evolving field, and we're committed to keeping our content up-to-date with the latest trends, technologies, and best practices. Community Support: Learning to code is more fun and effective when you're part of a supportive community. Join our online community forums to connect with fellow learners, share insights, and collaborate on projects. Get Started Today:



Recent Posts

Advance Js Fetch API Part-5

June-06-2025 21:23:08


Advance Js Promise Part-4

June-06-2025 11:35:05


Advance js oop part-3

June-06-2025 08:25:42


ExpressJs API With Image Crud-3

June-02-2025 21:09:41


NodeJs API-MVC-2

June-02-2025 10:56:22


ExpressJs API Crud-1

June-02-2025 10:27:58


ExpressJs Files upload

June-02-2025 10:15:40


NodeJS Cookie and CsrfToken

May-30-2025 19:20:40


ExpressJS Session

May-30-2025 19:14:29


ExpressJS NodeJs Crud Part-2

May-27-2025 21:52:18


ExpressJS NodeJS Crud Part-1

May-27-2025 21:33:30


ExpressJS Authentication

May-26-2025 17:11:41


NodeJS ExpressJS Files Upload

May-26-2025 16:53:29



Angular Introduction

May-13-2025 21:02:32



Advance Js part-1

May-12-2025 15:41:29