Async 非同步

Node.js Async 非同步

Promise

什麽是 Promise?

Promise 是一个表示非同步运算的最终 完成失败 的物件。Promise 物件会有三种状态::pending 初始状态 、 fulfilled 表示操作成功地完成 、 rejected 表示操作失败。如果我们需要依序串连执行多个 Promise 功能的话,可以透过 .then() 来做到。

The Promise object represents the eventual completion (or failure) of an asynchronous operation and its resulting value.

A promise may be in one of 3 different states: pending, fulfilled or rejected.

Promises help you naturally handle errors, and write cleaner code by not having callback parameters.

Async

什麽是 Async

async/await 其实是 promise 语法糖,让你可以用更简洁的方法达到非同步。(async function 本身就会回传 promise)

The async function is syntactic sugar for promise.

Async

参考资料