当前位置:网站首页>36 - new promise method: allsettled & any & race
36 - new promise method: allsettled & any & race
2022-07-27 07:16:00 【Front end code: Nong Xiaowang】
brief introduction
In the previous two articles , We have covered basic and advanced promise knowledge . There are also two nice new operators / Method , It can be that the code is simpler , Let's review them .
allSettled
ES2020 or ES11 Introduced promise.allSettled, So it is a very new method, so you need to be careful when using it . Before using, you need to determine the browser version you want to support .
When all promise Return whether success or failure ,allSettled Will return to one promise. The return value is an array of objects , Each of these describes the input promise The return value of .
allSettled and all There are some differences .
all Will return the entered promise The first is the result of failure , So if we enter 5 individual promise Two of them failed ,all Will return the first failed result .
allSettled Will wait for all promise Complete and return all the results corresponding to the input order . When asynchronous tasks have no dependencies on each other, use allSettled Then retry the failed task . Use if your execution steps depend on all asynchronous tasks all.
const promise1 = Promise.resolve("Parwinder");
const promise2 = new Promise((resolve) => {setTimeout(() => {resolve("Lauren");}, 2000);
});
const promise3 = Promise.reject("Robert");
const promise4 = Promise.resolve("Eliu");
Promise.allSettled([promise1, promise2, promise3, promise4]).then((data) => {console.log(data);
});
Once the above four tasks are completed ( Success or failure ),allSettled Will go to then The result is passed in the callback function of , Print as follows :
[{status: "fulfilled",value: "Parwinder"
}, {status: "fulfilled",value: "Lauren"
}, {reason: "Robert",status: "rejected"
}, {status: "fulfilled",value: "Eliu"
}]
any
any The input parameters of are usually iteratable objects such as arrays , It returns the first success in the array promise Result , If all promise If both fail, it returns AggregateError ,AggregateError Is used to combine input promise Independent error returned .
any And all Is relative .
const promise1 = Promise.resolve("Parwinder");
const promise2 = new Promise((resolve) => {setTimeout(() => {resolve("Lauren");}, 2000);
});
const promise3 = Promise.reject("Robert");
const promise4 = Promise.resolve("Eliu");
Promise.any([promise1, promise2, promise3, promise4]).then((data) => {console.log(data); // Parwinder (first successful promise)
});
Suppose that all promise All return errors :
const promise1 = Promise.reject("Parwinder");
const promise2 = new Promise((resolve,reject) => {setTimeout(() => {reject("Lauren");}, 2000);
});
const promise3 = Promise.reject("Robert");
const promise4 = Promise.reject("Eliu");
Promise.any([promise1, promise2, promise3, promise4]).then((data) => {console.log(data); // "AggregateError: No Promise in Promise.any was resolved"
});
race
race Will return the first success or failure promise Result .
const promise1 = new Promise((resolve, reject) => {setTimeout(resolve, 500, 'one');
});
const promise2 = new Promise((resolve, reject) => {setTimeout(resolve, 100, 'two');
});
Promise.race([promise1, promise2]).then((value) => {console.log(value);// Both resolve, but promise2 is faster
});
// expected output: "two"
If passed to race The parameter of is an empty array , There will never be a result :
var foreverPendingPromise = Promise.race([]);
console.log(foreverPendingPromise);
setTimeout(function(){console.log('the stack is now empty');console.log(foreverPendingPromise);
});
// logs, in order:
// Promise { <state>: "pending" }
// the stack is now empty
// Promise { <state>: "pending" }
边栏推荐
- 线性表 -- 栈和队列
- Cass11.0.0.4 for autocad2010-2023 dog free usage
- The possibility of metauniverse from the perspective of technical principles: how Omniverse "built" Mars
- flink cdc 抽取oracle的数据,会不断的占用oracle的内存吗,最后引发ora-040
- Analysis of strong tennis cup 2021 PWN competition -- babypwn
- "Weilai Cup" 2022 Niuke summer multi school training camp 1
- 指令集 x 数澜科技丨加速政企数字化转型,打造DT领域独角兽企业联盟
- Interpretation of deepsort source code (VII)
- PNA polypeptide PNA TPP | GLT ala ala Pro Leu PNA | suc ala Pro PNA | suc AAPL PNA | suc AAPM PNA
- Instruction set x digital technology accelerates the digital transformation of government and enterprises, and builds Unicorn enterprise alliance in DT field
猜你喜欢

Hospital reservation management system based on SSM

Basic statement of MySQL (1) - add, delete, modify and query

DNA modified zinc oxide | DNA modified gold nanoparticles | DNA coupled modified carbon nanomaterials

Pan Aimin, chairman of instruction set, attended the 2022 ecug con to speak for China's technical forces

Drools (5): drools basic syntax (3)

Campus news release management system based on SSM

DNA (deoxyribonucleic acid) supply | carbon nanotube nucleic acid loaded dna/rna material | dna/rna nucleic acid modified magnetic nanoparticles

Analysis on the current situation and optimization strategy of customer experience management in banking industry

Shell programming specifications and variables

基于SSM实现的校园新闻发布管理系统
随机推荐
基于SSM实现的校园新闻发布管理系统
Codeforces Round #787 (Div. 3)(7/7)
py2exe qt界面风格变成了win98解决方案
Word wrap: break word line feed is compatible with browsers
Digital image processing - Chapter 6 color image processing
Go obtains the processing results of goroutine through channel
2022-07-25 顾宇佳 学习笔记
12. Integer to Roman整数转罗马数字
Watermelon book chapter 3 - linear model learning notes
adb指令整理
Codeforces Round #804 (Div. 2)(5/5)
请问 mysql timestamp(6) 用flink-sql接过来是 null,这点有办法处理不
AI: play games in your spare time - earn it a small goal - [Alibaba security × ICDM 2022] large scale e-commerce map of risk commodity inspection competition
Shell programming specifications and variables
Datascience: data generation adds a small amount of noise (customizable noise) to the original data to realize the construction of new data (dataframe format data storage case)
[latex format] there are subtitles side by side on the left and right of double columns and double pictures, and subtitles are side by side up and down
Codeforces Round #804 (Div. 2)(5/5)
高级IO提纲
VIVO应用市场APP上架总结
Digital image processing Chapter 1 Introduction