Home Web Front-end JS Tutorial Simple usage of promise objects

Simple usage of promise objects

Sep 23, 2017 am 10:41 AM
promise usage Simple

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;
        });
    }
Copy after login


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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

The easiest way to query the hard drive serial number The easiest way to query the hard drive serial number Feb 26, 2024 pm 02:24 PM

The easiest way to query the hard drive serial number

Analyze the usage and classification of JSP comments Analyze the usage and classification of JSP comments Feb 01, 2024 am 08:01 AM

Analyze the usage and classification of JSP comments

How to correctly use the exit function in C language How to correctly use the exit function in C language Feb 18, 2024 pm 03:40 PM

How to correctly use the exit function in C language

How to write a simple student performance report generator using Java? How to write a simple student performance report generator using Java? Nov 03, 2023 pm 02:57 PM

How to write a simple student performance report generator using Java?

Usage of WPSdatedif function Usage of WPSdatedif function Feb 20, 2024 pm 10:27 PM

Usage of WPSdatedif function

Keeping your word: The pros and cons of delivering on your promises Keeping your word: The pros and cons of delivering on your promises Feb 18, 2024 pm 08:06 PM

Keeping your word: The pros and cons of delivering on your promises

Introduction to Python functions: Usage and examples of isinstance function Introduction to Python functions: Usage and examples of isinstance function Nov 04, 2023 pm 03:15 PM

Introduction to Python functions: Usage and examples of isinstance function

How to use Apple shortcuts How to use Apple shortcuts Feb 18, 2024 pm 05:22 PM

How to use Apple shortcuts

See all articles