Table of Contents
Link Commitment
结论
Home Web Front-end JS Tutorial JavaScript: Keeping our word

JavaScript: Keeping our word

Sep 01, 2023 pm 03:53 PM

JavaScript: Keeping our word

Through its popularity and recent improvements, JavaScript is increasingly becoming a web programmer’s best friend. Like all best friends, JavaScript keeps its promises.

This may sound a little strange, but it's true. Most browsers currently support so-called Promise objects. A Promise is much like a function that represents a piece of code or a task that you want to execute at some point in the future.

This is what commitment looks like.

var myPromise = new Promise(function (resolve, reject) {
    // Task to carry out goes here.
});
Copy after login

You can see here that when we create a Promise, we give it a parameter, which is a function that contains the code we want to execute at some point in the future. You may also notice the two parameters passed to the Promise in the function: resolve and reject. These are also functions, and are how we tell a Promise whether it's fulfilled. Here's how you use them:

var myPromise = new Promise(function (resolve, reject) {
    if (true) {
        resolve('Hello Tuts+ fans!');
    } else {
        reject('Aww, didn\'t work.');
    }
});
Copy after login

This promise will obviously always be resolved, since the if statement will always be true. This is just for learning purposes - we'll do something more practical later - but imagine replacing true with a piece of code that you're not 100% sure is valid.

Now that we have created a Promise, how do we use it? Well, we need to tell it what the resolve and reject functions are. We do this by using Promise’s then method.

myPromise.then(function (result) {
    // Resolve callback.
    console.log(result); 
}, function (result) {
    // Reject callback.
    console.error(result);
});
Copy after login

Because our if statement always passes its true check, the code above will always log "Hello Tuts fans!" to the console. It also executes immediately. This is because the code inside the Promise constructor is synchronous, meaning it does not wait for any operations to be performed. It has all the information it needs to proceed, and will do so as quickly as possible.

However, where Promise really shines is in asynchronous tasks, tasks where you don’t know when the Promise will actually be fulfilled. A real-world example of an asynchronous task is fetching a resource, such as a JSON file, via AJAX. We don't know how long it will take for the server to respond, or if it may even fail. Let's add some AJAX to our Promise code.

var myPromise = new Promise(function (resolve, reject) {
    // Standard AJAX request setup and load.
    var request = new XMLHttpRequest();
    
    // Request a user's comment from our fake blog.
    request.open('GET', 'http://jsonplaceholder.typicode.com/posts/1');

    // Set function to call when resource is loaded.
    request.onload = function () {
        if (request.status === 200) {
            resolve(request.response);
        } else {
            reject('Page loaded, but status not OK.');
        }
    };

    // Set function to call when loading fails.
    request.onerror = function () {
        reject('Aww, didn\'t work at all.');
    }

    request.send();
});
Copy after login

The code here is just standard JavaScript for performing AJAX requests. We request a resource, in this case a JSON file at a specified URL, and wait for it to respond. We'll never know exactly when. We obviously don't want to stop executing the script to wait for it, so what do we do?

Fortunately, we have put this code into a Promise. By putting it here, we're basically saying, "Hey, a piece of code, I have to go now, but I'll call you later and tell you when to execute it. Promise you're going to do it and tell me you When will it be done?" The code will say: "Yes, of course. I promise."

The important thing to note in the above code is the call of the resolve and reject functions. Remember, these are how we tell our promises whether our code executed successfully. Otherwise, we'll never know.

Using the same code from the basic example, we can see how AJAX requests in Promise will now work.

// Tell our promise to execute its code
// and tell us when it's done.
myPromise.then(function (result) {
    // Prints received JSON to the console.
    console.log(result);
}, function (result) {
    // Prints "Aww didn't work" or
    // "Page loaded, but status not OK."
    console.error(result); 
});
Copy after login

I know we can trust you, myPromise.

Now, you might think that Promise is just a fancy callback function with better syntax. This is true to an extent, but to continue with our AJAX example, let's say you need to make a few more requests, each based on the results of the previous request. Or what if you need to process JSON first?

Doing this using callbacks will result in a lot of nesting of functions, each becoming increasingly difficult to track. Luckily, in the world of Promises, we can chain these functions together like this. Here's an example, once we receive the JSON of a user's comment on our fake blog, we need to make sure it's all lowercase before doing anything else with it.

myPromise
    .then(function (result) {
        // Once we receive JSON,
        // turn it into a JSON object and return.
        return JSON.parse(result);
    })
    .then(function (parsedJSON) {
        // Once json has been parsed,
        // get the email address and make it lowercase.
        return parsedJSON.email.toLowerCase();
    })
    .then(function (emailAddress) {
        // Once text has been made lowercase,
        // print it to the console.
        console.log(emailAddress);
    }, function (err) {
        // Something in the above chain went wrong?
        // Print reject output.
        console.error(err);
    });
Copy after login

You can see here that although our initial call is asynchronous, it is also possible to chain synchronous calls. resolve The code in the function will be called on each then return. You will also notice that only one error function is specified here for the entire chain. By placing it at the end of the chain as the last then in the reject function, any Promise in the chain that calls reject will call this function one.

现在我们对承诺更有信心了,让我们结合上面的承诺创建另一个承诺。我们将创建一个采用新的小写电子邮件地址的电子邮件地址,并(假装)向该地址发送电子邮件。这只是一个说明异步内容的示例 - 它可以是任何内容,例如联系服务器以查看电子邮件是否在白名单上或者用户是否已登录。我们需要将电子邮件地址提供给新的 Promise,但承诺不接受争论。解决这个问题的方法是将 Promise 包装在一个可以执行此操作的函数中,如下所示:

var sendEmail = function (emailAddress) {
    return new Promise(function (resolve, reject) {
        // Pretend to send an email
        // or do something else asynchronous
        setTimeout(function () {
            resolve('Email sent to ' + emailAddress);
        }, 3000);
    });
};
Copy after login

我们在此处使用 setTimeout 调用来简单地伪造一个需要几秒钟才能异步运行的任务。

那么我们如何使用新的承诺创建函数呢?好吧,由于在 then 中使用的每个 resolve 函数都应该返回一个函数,那么我们可以以与同步任务类似的方式使用它。

myPromise
    .then(function (result) {
        return JSON.parse(result);
    })
    .then(function (parsedJSON) {
        return parsedJSON.email.toLowerCase();
    })
    .then(function (emailAddress) {
        return sendEmail(emailAddress)
    })
    .then(function (result) {
        // Outputs "Email sent to stuart@fakemail.biz"
        console.log(result);
    }, function (err) {
        console.error(err);
    });
Copy after login

让我们回顾一下这个流程,总结一下发生了什么。我们最初的 Promise myPromise 请求一段 JSON。当收到该 JSON 时(我们不知道何时),我们将 JSON 转换为 JavaScript 对象并返回该值。

完成后,我们从 JSON 中取出电子邮件地址并将其变为小写。然后我们向该地址发送一封电子邮件,同样我们不知道它何时完成,但当它完成时,我们将向控制台输出一条成功消息。看不到沉重的嵌套。

结论

我希望这是对 Promise 的有用介绍,并为您提供了在 JavaScript 项目中开始使用它们的充分理由。如果您想更详细地了解 Promise,请查看 Jake Archibald 关于这个主题的优秀 HTML5 Rocks 文章。

学习 JavaScript:完整指南

我们构建了一个完整的指南来帮助您学习 JavaScript,无论您是刚刚开始作为 Web 开发人员还是想探索更高级的主题。

The above is the detailed content of JavaScript: Keeping our word. 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 AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

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)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1253
24
JavaScript Engines: Comparing Implementations JavaScript Engines: Comparing Implementations Apr 13, 2025 am 12:05 AM

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

Python vs. JavaScript: The Learning Curve and Ease of Use Python vs. JavaScript: The Learning Curve and Ease of Use Apr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

From C/C   to JavaScript: How It All Works From C/C to JavaScript: How It All Works Apr 14, 2025 am 12:05 AM

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

JavaScript and the Web: Core Functionality and Use Cases JavaScript and the Web: Core Functionality and Use Cases Apr 18, 2025 am 12:19 AM

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

JavaScript in Action: Real-World Examples and Projects JavaScript in Action: Real-World Examples and Projects Apr 19, 2025 am 12:13 AM

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

Understanding the JavaScript Engine: Implementation Details Understanding the JavaScript Engine: Implementation Details Apr 17, 2025 am 12:05 AM

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python vs. JavaScript: Community, Libraries, and Resources Python vs. JavaScript: Community, Libraries, and Resources Apr 15, 2025 am 12:16 AM

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

Python vs. JavaScript: Development Environments and Tools Python vs. JavaScript: Development Environments and Tools Apr 26, 2025 am 12:09 AM

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.

See all articles