I have a function that returns a Promise.all() like this:
return Promise.all(promises).catch(err => console.log(err))
Then I try to log the values like this
myFunction()
.then(values => console.log(values));
The result I get is:
[
Response (22 MB) {
ok: true,
url: "XXX",
statusText: "OK",
redirected: false,
bodyUsed: false,
status: 200,
Blob (22 MB)
}, Response (22 MB) {
ok: true,
url: "XXX",
statusText: "OK",
redirected: false,
bodyUsed: false,
status: 200,
Blob (22 MB)
}, Response (21 MB) {
ok: true,
url: "XXX",
statusText: "OK",
redirected: false,
bodyUsed: false,
status: 200,
Blob (21 MB)
}, Response (22 MB) {
ok: true,
url: "XXX",
statusText: "OK",
redirected: false,
bodyUsed: false,
status: 200,
Blob (22 MB)
}, Response (22 MB) {
ok: true,
url: "XXX",
statusText: "OK",
redirected: false,
bodyUsed: false,
status: 200,
Blob (22 MB)
}, Response (20 MB) {
ok: true,
url: "XXX",
statusText: "OK",
redirected: false,
bodyUsed: false,
status: 200,
Blob (20 MB)
}, Response (23 MB) {
ok: true,
url: "XXX"
statusText: "OK",
redirected: false,
bodyUsed: false,
status: 200,
Blob (23 MB)
}
]
However, I cannot log the values whichever way I do it. With a regular Promise, I have always been able to do it using the await
operator. I have also tried it in this case, but all I get are the Response objects.
MDNs own example that can be seen HERE does not include the await
operator and they are still able to log the values:
const promise1 = Promise.resolve(3);
const promise2 = 42;
const promise3 = new Promise((resolve, reject) => {
setTimeout(resolve, 100, 'foo');
});
Promise.all([promise1, promise2, promise3]).then((values) => {
console.log(values);
});
// expected output: Array [3, 42, "foo"]
I suspect I am having this challenge because I am working with Promise.all()
and it somehow behaves differently compared to a normal promise.
Does anyone understand what I may be doing wrong?
Thank you for being so supportive.
UPDATE (FIX): I tried a different pattern with try... catch
that I found HERE, which worked. However, it annoys me that I cannot understand why.
One thing is that they use two Promise.all()
. Why is that?
They also use full await, although that should not make any difference