当前位置:网站首页>Handwritten simple promise
Handwritten simple promise
2022-06-11 23:31:00 【RxnNing】
export class newPromise {
constructor(executor) {
//new example Pass in a function
executor(this.resolve, this.reject)
}
status = PENDING
res = undefined
result = undefined
resolve = (value) => {
//resolve Method take pendding become fulfilled
if(this.status !== PENDING) return
this.status = FULFILLED
this.res = value
}
reject = result => {
//reject Method : take pendding become reject
if(this.status !==PENDING) return
this.status = REJECTED
this.result = result
}
then = (successCallback,failCallBack) => {
//then Method
if(this.status === FULFILLED) {
successCallback(this.value)
}else if(this.status == REJECTED) {
failCallBack(this.result)
}
}
}
Promise Execute as soon as new .then Callback function specified by method , It will not be executed until all synchronization tasks of the current script have been executed ,
In the above code ,promise Throw an error , I was catch() Method to capture . Be careful , The above is equivalent to the following two .
// Writing a
const promise = new Promise(function(resolve, reject) {
try {
throw new Error('test');
} catch(e) {
reject(e);
}
});
promise.catch(function(error) {
console.log(error);
});
// Write two
const promise = new Promise(function(resolve, reject) {
reject(new Error('test'));
});
promise.catch(function(error) {
console.log(error);
});
const someAsyncThing = function() {
return new Promise(function(resolve, reject) {
// The next line will report an error , because x No statement
resolve(x + 2);
});
};
someAsyncThing().then(function() {
console.log('everything is great');
});
setTimeout(() => {
console.log(123) }, 2000);
// Uncaught (in promise) ReferenceError: x is not defined
// 123
In the above code ,someAsyncThing() The function produces Promise object , There's a syntax error inside . The browser runs to this line , It will print out an error message ReferenceError: x is not defined, But it doesn't exit the process 、 Terminate script execution ,2 It's still going to output in seconds 123. That is to say ,Promise Internal errors do not affect Promise External code , The popular saying is “Promise Will eat the mistake ”.
p = Promise.all Method : Will be multiple promise example Pack into a new one promise example
Take an array as an argument
Two cases :
Only when all the states become fulfilled p Will become fulfilled Returns an array
Just one person rejected p It becomes reject The first failed is returned
p = Promise.rece() jiang duoge pormise example Pack into a new one pormise example
As long as one instance changes state p The state of follows the high side , The return value of the first changed instance
Parameter method and promise.all() yiyang rug bushi pormise example Will call first Promise.reoslve() Method Convert parameter to Promise example In further processing
边栏推荐
- CVPR 2022 | 元学习在图像回归任务的表现
- [Day9 literature extensive reading] on the (a) symmetry between the perception of time and space in large-scale environments
- Common problems of Converged Communication published in February | Yunxin small class
- 05 classification learning notes lihongyi's in-depth study 2021
- I2C read / write process
- 解决IDEA下载插件慢的问题
- 机器学习之数据处理与可视化【鸢尾花数据分类|特征属性比较】
- GMN of AI medicine article interpretation
- How to make scripts executable anywhere
- Leetcode must review 20 lintcode (5466421166978227)
猜你喜欢

Lake Shore - supervaritemp low temperature thermostat

通用树形结构的迭代与组合模式实现方案
![[Day8 literature extensive reading] space and time in the child's mind: evidence for a cross dimensional symmetry](/img/c2/e70e7c32c5dc5554dea29cb4627644.png)
[Day8 literature extensive reading] space and time in the child's mind: evidence for a cross dimensional symmetry

解决IDEA下载插件慢的问题

Unity3d C#开发微信小游戏音频/音效播放问题解决过程分享

Zigbee3.0 wireless packet capturing installation method based on e18-2g4u04b

MySQL 8.0 解压版安装教程

A new product with advanced product power, the new third generation Roewe rx5 blind subscription is opened

2022 safety officer-b certificate theoretical question bank and simulation test

Summary of personal wrong questions (the wrong questions have not been solved and are used for help)
随机推荐
我的创作纪念日
机器学习之数据处理与可视化【鸢尾花数据分类|特征属性比较】
Method for WiFi wireless transmission module to access cloud platform using esp8266 chip scheme
Application of Lora wireless communication module Lora technology in smart home light control
Pourquoi Google Search ne peut - il pas Pager indéfiniment?
The second bullet of in-depth dialogue with the container service ack distribution: how to build a hybrid cloud unified network plane with the help of hybridnet
[day6-7 intensive literature reading] a unifying Bayesian framework accounting for spatiotemporal interactions with a
C language simple exercise No.17, about the combination of for and while loops
Antigen products enter the family, and Chinese medical device enterprises usher in a new blue ocean
Research Report on development trend and competitive strategy of global wafer recycling and OEM service industry
帝国理工等最新《胶囊网络综述》论文,29页pdf阐述胶囊的概念、方法与应用
Why can't Google search page infinite?
Games-101 Yan Lingqi 5-6 lecture on raster processing (notes sorting)
[Delphi] determine the encoding method of the file (ANSI, Unicode, utf8, unicodebig)
[naturallanguageprocessing] [multimodal] albef: visual language representation learning based on momentum distillation
Here we go! Dragon lizard community enters PKU classroom
Stack (C language)
通用树形结构的迭代与组合模式实现方案
删除收货地址【项目 商城】
2022 low voltage electrician certificate and online simulation examination