异步 JavaScript

在本模块中,我们将了解 asynchronous JavaScript,为什么它很重要,以及如何使用它来有效处理潜在的阻塞操作,例如从服务器获取资源。

¥In this module, we take a look at asynchronous JavaScript, why it is important, and how it can be used to effectively handle potential blocking operations, such as fetching resources from a server.

先决条件

¥Prerequisites

异步 JavaScript 是一个相当高级的主题,建议你在尝试此操作之前先完成 JavaScript 第一步JavaScript 构建块 模块。

¥Asynchronous JavaScript is a fairly advanced topic, and you are advised to work through JavaScript first steps and JavaScript building blocks modules before attempting this.

注意:如果你正在使用无法创建自己的文件的计算机/平板电脑/其他设备,则可以尝试在线编码程序(例如 JSBinGlitch)中的(大部分)代码示例。

¥Note: If you are working on a computer/tablet/other device where you don't have the ability to create your own files, you can try out (most of) the code examples in an online coding program such as JSBin or Glitch.

指南

¥Guides

引入异步 JavaScript

在本文中,我们将了解同步和异步编程、为什么我们经常需要使用异步技术,以及与历史上在 JavaScript 中实现异步函数的方式相关的问题。

如何使用 promise

这里我们将介绍 Promise 并展示如何使用基于 Promise 的 API。我们还将介绍 asyncawait 关键字。

实现基于 Promise 的 API

本文将概述如何实现你自己的基于 Promise 的 API。

介绍工作线程

Workers 使你能够在单独的线程中运行某些任务,以保持主代码的响应能力。在本文中,我们将重写一个长时间运行的同步函数以使用工作线程。

评估

¥Assessments

动画排序

该评估要求你使用 Promise 以特定顺序播放一组动画。

也可以看看

¥See also