当前位置:网站首页>Application of higher-order functions: handwritten promise source code (III)
Application of higher-order functions: handwritten promise source code (III)
2022-07-23 11:18:00 【Vivqst】
Optimize :
1、 Realization Promise.then You can chain call
2、Promise.then It's an asynchronous task
// promise/A+ The specification States promise There are three states , And once the state is changed, it cannot change again
const PENDING = 'Pending'
const FULFILLED = 'Fulfilled'
const REJECTED = 'Rejected'
class Promise {
// Higher order function , The argument to a function is a function , Higher order functions can realize the preset of parameters
// The function in the parameter of the function is the declaration of the function
// executor Is a function that executes immediately , Parameters are preset in constructor in
constructor(executor) {
this.state = PENDING
this.value = undefined // Successful results
this.reason = undefined // The reason for failure
// solve then Problems that can be called multiple times
this.onResolvedCallbacks = []
this.onRejectedCallbacks = []
const resolve = (value) => {
if(this.state === PENDING) {
this.state = FULFILLED
this.value = value
// When the state changes, execute the callback function
this.onResolvedCallbacks.forEach(cb => cb(value))
}
}
const reject = (reason) => {
if(this.state === PENDING) {
this.state = REJECTED
this.reason = reason
// When the state changes, execute the callback function
this.onRejectedCallbacks.forEach(cb => cb(reason))
}
}
// Here is executor Function call . Parameters of the incoming resolve and reject Two functions
try {
executor(resolve, reject)
} catch(err) {
reject(err)
}
}
then(onFulfilledCallback, onRejectedCallback) {
onFulfilledCallback = typeof onFulfilledCallback === 'function' ? onFulfilledCallback : value => value
onRejectedCallback = typeof onRejectedCallback === 'function' ? onRejectedCallback : reason => reason
// You can chain call , be then It must be a Promise
return new Promise((resolve, reject) => {
// utilize setTimeout Solve the asynchronous problem
if(this.state === FULFILLED) {
setTimeout(() => {
let res = onFulfilledCallback(this.value)
resolve(res)
})
}
if(this.state === REJECTED) {
setTimeout(() => {
let err = onRejectedCallback(this.reason)
reject(err)
})
}
// When executor When an asynchronous task occurs ,then When it comes to execution , The state has not changed , You can put the callback into the callback function array
if(this.state === PENDING) {
this.onResolvedCallbacks.push((value) => {
setTimeout(() => {
let res = onFulfilledCallback(value)
resolve(res)
})
})
this.onRejectedCallbacks.push((reason) => {
setTimeout(() => {
let err = onRejectedCallback(reason)
reject(err)
})
})
}
})
}
}
let p = new Promise((resolve, reject) => {
console.log(2)
setTimeout(() => {
resolve(1)
}, 0)
})
p.then(res => {
console.log(res)
return 2
}).then(res => {
console.log(res)
console.log(3)
})
console.log(5)
// 2, 5, 1, 2, 3
边栏推荐
- Fun code rain, share it online~-
- [部署]presto-server-0.261.tar.gz的集群部署和启动
- [Python flask note 5] Blueprint simple à utiliser
- shell/sh/bash的区别和基本操作
- pyspark学习笔记
- Mixed view base class
- C语言之二分查找法或折半查找法剖析(经典例题,经典解析)
- 手写Promise.resolve,Promise.reject, Promise.all
- DWI图像 从DICOM Tag识别 b value 的方法
- Scattered notes of machine learning: some concepts and notes
猜你喜欢

Error when PLSQL creates Oracle Database: when using database control to configure the database, it is required to configure the listener in the current Oracle home directory. You must run netca to co

Spark common interview questions sorting

C语言中的分支和循环语句归属

JDBC的學習以及簡單封裝

【uiautomation】键指令大全(以及三种调用方式)+常用鼠标动作+SendKeys+Inspect学习

BurpSuite学习笔记

Pyspark learning notes

牛客刷题记录--Mysql

大厂面试机器学习算法(5)推荐系统算法

Install pyGame using CMD
随机推荐
[Doris]配置和基本使用contens系统(有时间继续补充内容)
JS call, apply, bind
Sorting out common SQL interview questions and answers
Redis数据库和项目框架
Dictionary creation and copying
C语言中的分支和循环语句归属
uni-app小程序中v-show与display:flex一起使用时v-show不生效!
同步发送短信验证码
TypeScript介绍
Redis database and project framework
Oracle创建数据库“监听程序未启动或数据库服务未注册”错误处理
Differences and basic operations of shell/sh/bash
机器学习中的矩阵向量求导
Spectral clustering | Laplace matrix
字典创建与复制
Pywinauto+某应用程序(学习至第9讲)--受阻
混入视图基类
[untitled]
JDBC Learning and simple Encapsulation
Markdown common syntax records