admin管理员组文章数量:1405731
var p1 = new Promise((resolve, reject) => {
resolve('p1');
});
var p2 = new Promise((resolve, reject) => {
resolve('p2');
});
Promise.all([
p1.finally(() => { console.log("p1 pleted"); }),
p2.finally(() => { console.log("p2 pleted"); }),
]).then(values => {
console.log(values[0]);
console.log(values[1]);
}).finally(() => {
console.log("all() pleted");
var p1 = new Promise((resolve, reject) => {
resolve('p1');
});
var p2 = new Promise((resolve, reject) => {
resolve('p2');
});
Promise.all([
p1.finally(() => { console.log("p1 pleted"); }),
p2.finally(() => { console.log("p2 pleted"); }),
]).then(values => {
console.log(values[0]);
console.log(values[1]);
}).finally(() => {
console.log("all() pleted");
I think I've only seen examples on the web with a single .finally() at the end [1]: https://i.sstatic/HeQV8.png
Share edited Sep 30, 2021 at 23:10 asked Sep 30, 2021 at 22:01 user8242629user8242629 4- post the code snippet here, not the image – Sachin Ananthakumar Commented Sep 30, 2021 at 22:08
- 1 DO NOT post images of code, data, error messages, etc. - copy or type the text into the question. Please reserve the use of images for diagrams or demonstrating rendering bugs, things that are impossible to describe accurately via text. For more information please see the Meta FAQ entry Why not upload images of code/errors when asking a question? – VLAZ Commented Sep 30, 2021 at 22:10
- Paste that into the question, not a ment. – Barmar Commented Sep 30, 2021 at 22:12
- You can use a Stack Snippet to make it executable. – Barmar Commented Sep 30, 2021 at 22:12
2 Answers
Reset to default 5You may chain as many .finally()
calls as you like onto any promise.
(Promise.all() returns a new promise, so this rule applies here as well.)
Run this, and you should see all 3 ments log.
Promise.resolve().
finally(() => console.log('Finally #1')).
finally(() => console.log('Finally #2')).
finally(() => console.log('Finally #3'))
Sure, finally
is a chainable promise method just like catch
(with the only difference that its callback does not take a parameter). You can use it as many times as you want, on any promise.
Promise.all([
p1.finally(() => { console.log("p1 pleted"); }),
p2.finally(() => { console.log("p2 pleted"); }),
]).finally(() => {
console.log("all() pleted");
})
本文标签: javascriptCan I have multiple finally() as well as multiple catch() in Promiseall()Stack Overflow
版权声明:本文标题:javascript - Can I have multiple .finally() as well as multiple .catch() in Promise.all()? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744932622a2632989.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论