Showing posts with label javascript pro. Show all posts
Showing posts with label javascript pro. Show all posts

Tuesday, April 14, 2026

Promises in JavaScript



 Create Promise

let myPromise = new Promise(function(resolve, reject) {

Condition

let success = true;

If Condition

if (success) {

    resolve("Task Completed");

}

Else Condition

else {

    reject("Task Failed");

}

Handle Success

.then(function(result) {

    console.log(result);

})

Handle Error

.catch(function(error) {

    console.log(error);

});


Full Code :

let myPromise = new Promise(function(resolve, reject) {

    let success = true;


    if (success) {

        resolve("Task Completed");

    } else {

        reject("Task Failed");

    }

});


myPromise

.then(function(result) {

    console.log(result);

})

.catch(function(error) {

    console.log(error);

});

⚡ Flow Summary

Promise created

Check condition (success)

If true → resolve() → then()
If false → reject() → catch()


🎯 Real-life Example


  • You ordered food πŸ”
  • If delivered →
  • If delivered → resolve("Food arrived")
  • If not →
  • If not → reject("Delivery failed")

πŸš€ Why Flutter & Dart Are the Future of App Development

Want to build Android, iPhone, Web, and Desktop apps using a single codebase? That’s exactly what Flutter and Dart make possible. πŸ“± What is...