PROMISE in JAVASCRIPT

Photo by Max Chen on Unsplash

Asynchronous programming in Javascript

var xxxImage = ajax.get("hotgirl.info");
console.log(xxxImage);
// Pass callback to function ajax
var callback = function (image) {
console.log (image);
};
ajax.get("hotgirl.info", callback);

// Can be abbreviated as follows
ajax.get("hotgirl.info", function (image) {
console.log (image);
})
var car = buycar();
var girl = goout(car);
var abcd = goToHotel(y);

// That must use callback heap, creating callback hell
buycar(function(car) {
goout(car, function(girl) {
goToHotel(hotel, function(z) {
// Do something, who knows
});
});
});

What is a Promise?

Photo by Katrina Holmgren on Unsplash

A promise has the following 3 states:

  • pending
  • fulfilled
  • reject
function get (url) {
return new Promise ((resolve, reject) => {
// Get the picture from abc.com
// If there is an error, I can not promise
if (error) reject ("Error");

// If obtained, then fulfill the promise
resolve (xxxImage);
});
}

var promise = ajax.get ("hotgirl.info");
promise
.then (image => fap)
.catch ((error) => alert (error));

Why we should use PROMISE?

  • Promise supports “chaining”
  • Promises make it easier to catch errors
  • Handling asynchronous

1. Promise chaining

// Dùng callback hell
buycar(function(car) {
goout(car, function(girl) {
goToHotel(hotel, function(z) {
// Do something, who knows
});
});
});

// Use promises, lightweight and easy to read code
buycar
.then(goout)
.then(goToHotel)
.then(function() { /*Do something, who knows*/ });

2. Promises make it easier to catch errors

// When a function fails, the promise is rejected.
function send_hotel () {
return Promise ((response, reject) => {
reject ("Sorry today, I am red light");
});
}


please_mother_mua_xe
.then (played_player)
.then (hotel_to_hotel)
.then (function () {/ * Do something, who knows * /})
.catch (function (err) {
console.log (err); // "Sorry today I light red"
console.log ("bad luck");
});

3. Asynchronous handling

Conclusion

--

--

--

Always be nice to anybody who has access to my toothbrush.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

A Brief Overview of React Router and Client-Side Routing

Melon Ali JavaScript article

Sub categories — How to build an Ecommerce website using Django 3 and Vue.js — Part 23

3/17 Higher-order Functions

How To Do More Development in Less Time? Have Meetings in the Day and Write Code at Night

Manage webhooks with MongoDB Functions and Triggers

Shopify webhooks on NodeJS Express

How to Integrate GraphQL With React

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Beribey

Beribey

Always be nice to anybody who has access to my toothbrush.

More from Medium

Javascript manually coded Google Maps

Give Me 5 Minutes — and I’ll Teach You How to Find the Time Complexity of an Algorithm

Javascript: The Call Stack and Scope

JavaScript Concepts(Closures, Conversion)