Promises are great to work with! Or so does your fellow developer at work says.
This article would give you to the point no bullshit tips on ho...
For further actions, you may consider blocking this person and/or reporting abuse
Nice. I may be in the 0.01% that needs to use
.then(x, y)
so thanks for explaining the differenceAlso in your first example, I'm pretty sure
...can be simplified to
... because of function currying, assuming
ServerStatusPromise
accepts one value and returns one value.This means you can do things like:
I agree that that code can be simplified into the one you mentioned, but in my humble opinion:
this
) sensitive. I have seen folks doing.catch(console.error.bind(console))
just to avoid that, when they could simply have used a.catch((err)=> console.error(err))
.Hi Kushan,
Thanks for having taken the time to write this, great post.
Just a couple of things that they are not correct:
Hey Mauro,
Thanks for giving the feedback.
Yes I agree, I think I tried to convey that it doesn't do any sort of batch execution, but then promises by design run the moment they are created. I think I should have an additional tip which clarifies about how promises run.
Whoops, missed that, corrected it :)
Hi, since we're talking of Promise, these days I'm publishing a series of videos about how to build a promise polyfill, with the stated goal of making clear how promise works under the hood.
If this sounds as something interesting, here's the first video: youtube.com/watch?v=E_p-PVNqhZE
Thanks so much for this! I never knew about
Promise.reject
/Promise.resolve
. Awesome :DHi, hope you can help me.
having this:
async searchDatoReg(locclave: string, locmail: string) {
this.mensaje = null;
return new Promise((resolve) => {
this.api.searchProductoLg(locclave, locmail).subscribe(
(data) => {
if (isUndefined(data[0])) {
this.op = 0;
this.filtrados = null;
console.log('tempx1:');
} else {
this.filtrados = { ...data };
this.op = 1;
console.log('tempx2:');
}
then:
await this.searchDatoReg(locclave, locmail);
dosent syncronize the call, should first do this.searchDatoReg(..) then the rest
but never does it
any hint please
Thanks so much for the post. I always love learning more about JavaScript. In fact, I recently created a topsites site that I think this link belongs on. It is at ciphly.com?languages=JavaScript. Please consider adding this post and other informative ones like it!
Hi Kushan,
Thanks for explaining the promise in a very good way especially Promise constructor.
I have a question.
In promise all. I have five promise which runs concurrently. If promise one resolve and promise two got a problem will go directly to catch part of Promise.all. does it rollback the promise one changes or not.
I am a bit confused.
Or let's say. In Promise.all, if an error occurs to single promise. does it rollback all other promises changes?
Thanks in advance.
I am not clear what you mean by rolling back.
Let us look at a modified point 7's example:
If I understand your doubt correctly, imagine the above situation in which there are 2 promises
prom1
andprom2
. Now ifprom2
fails becausefetchPosts
fails, the following things will happen:console.log(comments)
would be shown to users, since promises are immutable andprom1
has nothing to do withprom2
failing.console.log(posts)
would NOT be shown to users, sinceprom2
has failed.console.log('Hello')
would NOT be shown to users, since it is a new promise composed ofprom1
&prom2
, in whichprom2
has failed.To understand
Promise.all
correctly. You need to understand thatPromise.all
creates a new promise whose outcome is dependant on the promises (eg. prom1 and prom2) it was composed with.Let us use our previous example and this time name the new promise:
The outcome of
resultPromise
is:resolved
: If all of the promises resolve.rejected
: If one or more promises rejects. In our exampleprom2
rejected and henceresultPromise
would also be rejected.Now you can clearly see the dependency of all these promises and their outcome. (btw reread the point 2, I hope this comment will make it easier).
thanks for this post! Gave me some idea how to understand Promises on open-source projects :D
Hi kushan,
Thanks for sharing, this post helps me a lot.
Best promise article ever
Thank you very much... You great
Very good read! Finally a simple piece of text that explains how to do stuff and not how to not do stuff.
That is a very informative post on Promises.
Thanks for writing :)
Learnt a lot. Thanks Kushan!