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?
Related
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Get the value of a Javascript Promise in a synchronous way [duplicate]
(2 answers)
How can I synchronously determine a JavaScript Promise's state?
(28 answers)
Using resolved promise data synchronously
(3 answers)
How to wait for a promise to be resolved?
(3 answers)
Closed 1 year ago.
Is it possible to access the value that represents the promise object directly?
Promise { <state>: "fulfilled", <value>: {…} }
<state>: "fulfilled"
<value>: Object { code: 3000, data: (15) […] }
Thanks!
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:
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.
This question already has an answer here:
Is there a difference between promise.then.then vs promise.then; promise.then [duplicate]
(1 answer)
Closed 5 years ago.
Is there a difference between these two ways for returning promise from a function
var promise;
promise = callAsync();
promise.then(doSomething).then(doSomethingElse);
return promise;
vs
var promise;
promise = callAsync();
return promise.then(doSomething).then(doSomethingElse);
I thought both approaches are same, but in a mocha test case, only second approach is working.
They are certainly not the same. Each .then() returns a new promise. So,
return promise;
is returning the original promise, but:
return promise.then(doSomething).then(doSomethingElse);
is returning a new promise that is the result of calling both .then() methods. The difference is that the latter one is returning a promise that is influenced by the functions called in those .then() methods, whereas the first promise is only influence by callAsync() and has nothing to do with the other .then() handlers.
The first promise is only monitoring callAsync(). It has absolutely nothing to do with what happens in other .then() handlers. The key to understanding .then() is that it returns a new promise and it is that new promise that is influenced by what happens in the .then() handlers.
For more info, read this answer: Is there a difference between promise.then.then vs promise.then; promise.then to understand the difference between chaining and branching.
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.