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");
})
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744932623a4601827.html
评论列表(0条)