Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

手写一个Promise #61

Open
GGXXMM opened this issue Sep 22, 2019 · 0 comments
Open

手写一个Promise #61

GGXXMM opened this issue Sep 22, 2019 · 0 comments
Labels
es6 es6 knowledge

Comments

@GGXXMM
Copy link
Owner

GGXXMM commented Sep 22, 2019

一、Promise的含义

Promise是一种异步编程的解决方案,它最早由社区提出和实现,ES6将其写进了语言标准,统一了用法,原生提供了Promise对象。

所谓Promise,简单说就是一个容器,里面保存着某个未来才会结束的事件(通常是一个异步操作)的结果。从语法上说,Promise 是一个对象,从它可以获取异步操作的消息。Promise 提供统一的 API,各种异步操作都可以用同样的方法进行处理。

二、Promise的基本用法

Promise.then()

为 Promise 实例添加状态改变时的回调函数。

Promise.catch()

用于指定发生错误时的回调函数。

Promise.finally()

不管promise最后的状态,在执行完then或catch指定的回调函数以后,都会执行finally方法指定的回调函数。

Promise.all()

Promise.race()

Promise.allSettled()

Promise.any()

Promise.resolve()

Promise.reject()

参考:https://es6.ruanyifeng.com/#docs/promise

三、Promise的状态(3种)

  • pendding 等待状态
  • fulfilled 已成功
  • rejected 已失败

四、手写实现Promise

@GGXXMM GGXXMM added the es6 es6 knowledge label Dec 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
es6 es6 knowledge
Projects
None yet
Development

No branches or pull requests

1 participant