After values are sent to allSettled, they are resolved or rejected in a promise.
How can I differentiate which values are rejected? Is there any way to know the value of these?
In console it only shows that it has been rejected but not its value.
I want rejected to say “value: 30” as it appears when the promise is fulfilled.
let arr = [30, 15]
Promise.allSettled(arr)
.then(responses => {
responses.forEach((resp) => {
console.log(resp)
})
}).catch(err => {
console.log(err)
})
The status doesn’t do it?
You can try something like this:
First define a custom Error
class, with a field with the value you want to work with, for example id
:
class MyCustomError extends Error {
constructor(message, id) {
super(message, id);
this.name = 'My Custom Error';
this.id = id
}
}
Use this error class with your code like this:
function process_all_promises() {
const arr = [
Promise.resolve({ id: 1}),
Promise.reject(new MyCustomError('Error due to some reason with id', 15),
{ id: 99 }
];
Promise.allSettled(arr)
.then(responses => {
responses.forEach((resp) => {
if (resp.status === 'rejected') {
console.log('@ Reason:', JSON.stringify(resp.reason));
}
else {
console.log('@ Success:', resp.value);
}
})
}).catch(err => {
console.log('Promise all errors', err);
});
}
The value you want to capture is in the id
field of the rejected promise’s reason object.
Not in rejected, look, there is no “job_application_id”.
system
Closed
January 31, 2023, 12:38am
5
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.