# asynchronous programming

异步(Asynchronous, async)是与同步(Synchronous, sync)相对的概念。

💥 异步编程 (opens new window)

# 异步编程

# Promises/A+

Promises/A+ (opens new window),An open standard for sound.

# 1. 概念

一个promise必须是三种状态之一:pendingfulfilledrejected

# 1.1 Promise状态

  • When pending, a promise:

    may transition to either the fulfilled or rejected state.

  • When fulfilled, a promise:

    • must not transition to any other state.

    • must have a value, which must not change.

  • When rejected, a promise:

    • must not transition to any other state.
    • must have a reason, which must not change.

# 1.2 then方法

一个Promise必须提供一个 then 方法去访问当前的最终值或reason,Promise的then方法接收两个参数:

promise.then(onFulfilled, onRejected)

# async/await

结论

  • 执行 async 函数,返回的都是 Promise 对象
  • Promise.then 成功的情况,对应 await
  • Promise.catch 异常的情况,对应 try...catch

https://www.bilibili.com/video/BV1mb4y1D7hH

Last Updated: 7/15/2021, 11:46:08 AM