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

Inheritance in JavaScript

Creating 2D texture on a 3D shape in p5.js.

Why React? Why Does React Work the Way It Works?

Creating an Alexa Game: Content Management with Sanity.io

How to add Vue (or another framework) to your React Micro Frontend

Core Interview Question and Answer

How to Add Analytics to your React Application

A Fair Comparison Between ReactJs Vs. React Native

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

Glance at the JavaScript arrow function

JavaScript Eğitimi - 1 / Değişkenler, Operatörler ve Kontrol Yapıları

Must know iterating array methods for any javascript interview!

JS-11 — JavaScript for Programmers, Chapter9, Part1