Simple usage of promise objects
The promise object is a solution proposed in es6 to solve asynchronous callbacks. As a newbie, I have just figured out this thing recently. I am writing this article, hoping to get some advice from experts. At the same time, I also think it will be helpful to novice friends who don’t understand promises.
No more nonsense, I won’t say much about introducing promises. If you don’t understand, you can search it yourself. This article only writes a simple example of promise. I believe I have seen some promises. I am a friend, but I am quite afraid of him (because when I didn’t understand this thing before, I thought it was very high-end). After seeing the examples, I can have a new understanding of him. Next, I directly attach a simple ajax request I wrote:
function get(url) { return new Promise((resolve, reject) => { var ajax = new XMLHttpRequest(); ajax.open('GET', url); ajax.onreadystatechange = function() { if (ajax.readyState == 4) { if(ajax.status == 200){ resolve(ajax); }else{ alert(2); reject(); } } } ajax.send(); }); } document.getElementById('btn').onclick = function() { get('b.json').then(function(res) { console.log(res.responseText); document.getElementById('p1').innerHTML = res.responseText; }); }
Because it introduces es6 objects, this article uses some es6 syntax. If any students don’t understand it, you can do it by yourself Baidu, the promise object receives two parameters, resolve and reject. My personal understanding is success and failure (if my understanding is wrong, I hope someone can correct me, after all, I just learned it). I don’t know many steps of ajax. Having said that, we return the promise object directly in the get function, connect our ajax method to this promise object, and finally the ajax request is successful. At this time, resolve comes in handy, resolve(ajax); and then it is over. If it is unsuccessful, just reject() directly (equivalent to request failure).
Finally, this simple case of promise is completed. You can test it in your own environment
The above is the detailed content of Simple usage of promise objects. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The hard disk serial number is an important identifier of the hard disk and is usually used to uniquely identify the hard disk and identify the hardware. In some cases, we may need to query the hard drive serial number, such as when installing an operating system, finding the correct device driver, or performing hard drive repairs. This article will introduce some simple methods to help you check the hard drive serial number. Method 1: Use Windows Command Prompt to open the command prompt. In Windows system, press Win+R keys, enter "cmd" and press Enter key to open the command

Classification and Usage Analysis of JSP Comments JSP comments are divided into two types: single-line comments: ending with, only a single line of code can be commented. Multi-line comments: starting with /* and ending with */, you can comment multiple lines of code. Single-line comment example Multi-line comment example/**This is a multi-line comment*Can comment on multiple lines of code*/Usage of JSP comments JSP comments can be used to comment JSP code to make it easier to read

How to write a simple student performance report generator using Java? Student Performance Report Generator is a tool that helps teachers or educators quickly generate student performance reports. This article will introduce how to use Java to write a simple student performance report generator. First, we need to define the student object and student grade object. The student object contains basic information such as the student's name and student number, while the student score object contains information such as the student's subject scores and average grade. The following is the definition of a simple student object: public

WPS is a commonly used office software suite, and the WPS table function is widely used for data processing and calculations. In the WPS table, there is a very useful function, the DATEDIF function, which is used to calculate the time difference between two dates. The DATEDIF function is the abbreviation of the English word DateDifference. Its syntax is as follows: DATEDIF(start_date,end_date,unit) where start_date represents the starting date.

In daily life, we often encounter problems between promises and fulfillment. Whether in a personal relationship or a business transaction, delivering on promises is key to building trust. However, the pros and cons of commitment are often controversial. This article will explore the pros and cons of commitments and give some advice on how to keep your word. The promised benefits are obvious. First, commitment builds trust. When a person keeps his word, he makes others believe that he is a trustworthy person. Trust is the bond established between people, which can make people more

How to use the exit function in C language requires specific code examples. In C language, we often need to terminate the execution of the program early in the program, or exit the program under certain conditions. C language provides the exit() function to implement this function. This article will introduce the usage of exit() function and provide corresponding code examples. The exit() function is a standard library function in C language and is included in the header file. Its function is to terminate the execution of the program, and can take an integer

Introduction to Python functions: Usage and examples of the isinstance function Python is a powerful programming language that provides many built-in functions to make programming more convenient and efficient. One of the very useful built-in functions is the isinstance() function. This article will introduce the usage and examples of the isinstance function and provide specific code examples. The isinstance() function is used to determine whether an object is an instance of a specified class or type. The syntax of this function is as follows

Introduction to Python functions: usage and examples of the abs function 1. Introduction to the usage of the abs function In Python, the abs function is a built-in function used to calculate the absolute value of a given value. It can accept a numeric argument and return the absolute value of that number. The basic syntax of the abs function is as follows: abs(x) where x is the numerical parameter to calculate the absolute value, which can be an integer or a floating point number. 2. Examples of abs function Below we will show the usage of abs function through some specific examples: Example 1: Calculation
