This question already has answers here:
Handling errors in Promise.all
(22 answers)
Wait until all promises complete even if some rejected
(20 answers)
Closed 5 years ago.
How can I handle errors from other than first promise rejection?
Example:
Promise.all[p1, p2, p3].then(...)
.catch((error) => { console.log(error) })
Order of rejection:
p1
p2
p3
Is it possible to get errors from p2 and p3?
EDIT:
from comments below: is it possible to catch error from specific Promise before passing it to .all? E.g. I want to stay with Promise.all funcionality but log all error cases also
There will be only one rejected promise. No more. You'll receive error for the first one rejected and that's it.
Related
This question already has answers here:
console.log of element.children shows 0 length but has three entries when expanded later
(1 answer)
Is there a way to tell if an ES6 promise is fulfilled/rejected/resolved? [duplicate]
(1 answer)
Api call using fetch with method get
(2 answers)
Closed 19 days ago.
Having the usual fake api fetch request and call console.log() immediately.
const url = "https://jsonplaceholder.typicode.com/users";
const promise = fetch(url);
console.log(promise);
That returns the following:
PromiseĀ {<pending>}
[[PromiseState]]: "fulfilled"
The question is:
How do we already know (in synchronous console.log() operation) the state of the promise is fulfilled or rejected? And why the promise is pending and fulfilled at the same time?
This question already has answers here:
What's wrong with awaiting a promise chain?
(3 answers)
Correct Try...Catch Syntax Using Async/Await
(6 answers)
Closed 1 year ago.
I have a doubt I wrote a program and I am confused what to use to handle if any error occur in my loop and which is best practice to write to handle error handling
should I use then catch or try catch in my for of loop as the output
for (value of Data){
test = await getValue(value)
.then((obj)=>{
// some code})
.catch((err)=>{
console.log(err);});
}
for (value of Data){
try{
test= await getValue(value);
}
catch (e){
console.log(e);
}
Ps: down vote welcome but need proper explanation which is best practise to write
.catch() vs. try/catch is somewhat a personal preference and it also depends upon how you want the code to run. Usually, you would use try/catch with await and .catch() when not using await, but there are exceptions. Also, you would generally NOT use .then() when using await. The whole point of await is to avoid .then() and the nested code it causes.
Inside, your for loop, await without a .then() vs. .then() with await gives completely different results. One offers parallel running of asynchronous operations, the other offers sequential running of asynchronous operations as the for loop pauses until the await fulfills.
So, you use the one that gives you the behavior you want. Then, pick the error handling method that matches (try/catch with await and .catch() with .then() - usually).
This question already has answers here:
Return value from a Promise constructor
(2 answers)
Closed 2 years ago.
I am relatively new to javascript and want to find out how the things here work.
I don't understand why does it output
testing.js:2 Uncaught (in promise) Oops instead of catching this error and handling it.
Everything works fine if I just type reject(..)
Here is the code:
let alex = new Promise((resolve,reject)=>{
return Promise.reject('Oops');
})
.then((r)=>console.log(r))
.catch((err)=>console.log(err));
That's because you should use the two arguments received in the callback (resolve and reject). As you said, it works when you use reject('Oops') instead of return Promise.reject('Oops'). It should look like this:
let alex = new Promise((resolve,reject)=>{
reject('Oops');
})
.then((r)=>console.log(r))
.catch((err)=>console.log(err));
You can read more about promises here
This question already has answers here:
Wait until all promises complete even if some rejected
(20 answers)
Closed 2 years ago.
It seems Promise.all() resolves as soon as one of the promises fail, I would like to run all promises even if some reject. Is there a function for this?
You can use Promise.allSettled(), the new Promise API would resolve when all the promise objects in the supplied array are settled (i.e. either fulfilled or rejected).
The value in the then callback would have an array of the objects having two keys status and value describing the result of each individual promise in the given array:
Promise.allSettled([
Promise.resolve("Resolved Immediately"),
new Promise((res, rej) => {
setTimeout(res("Resolved after 3 secs"), 3000)
}),
Promise.reject(new Error("Rejected Immediately"))
]).then(arr => console.log(arr));
This question already has answers here:
$.Deferred: How to detect when every promise has been executed
(3 answers)
Closed 9 years ago.
I am using jquery for promises.
I have a scenario, where i have two promises.
If promise1 rejects or resolve then system1 should reject or resolve respectively.
If promise2 rejects or resolve, system2 should resolve.
Function X should be called after both are settled and both system1 and system2 has resolved.
I tried :
var dp = $.when(promise1, promise2);
dp.done(function(one,two){
X();
}).fail(function(){
// promise1 might have not settled as of yet.
Should call X or not ?
});
But it returns as soon as one of the promise fails. So my promise1 is not resolved at the time when my fail is called.
How do i do it ?
The behaviour is exactly as per the documentation: The answer for your question is also in the documentation as italised.
In the multiple-Deferreds case where one of the Deferreds is rejected,
jQuery.when immediately fires the failCallbacks for its master
Deferred. Note that some of the Deferreds may still be unresolved at
that point. If you need to perform additional processing for this
case, such as canceling any unfinished ajax requests, you can keep
references to the underlying jqXHR objects in a closure and
inspect/cancel them in the failCallback.