panzerdp / dmitripavlutin.com-comments

7 stars 0 forks source link

promise-all-settled/ #134

Open utterances-bot opened 2 years ago

utterances-bot commented 2 years ago

How to Use Promise.allSettled()

How to use Promise.allSettled() to perform parallel async operations.

https://dmitripavlutin.com/promise-all-settled/

1nsayt commented 2 years ago

Sir, I thnik that you misprinted in the screen (https://dmitripavlutin.com/promise-all-settled/#21-all-promises-fulfilled) need to change const statuses = await productsPromise; to const statuses = await statusesPromise;

panzerdp commented 2 years ago

Sir, I thnik that you misprinted in the screen (https://dmitripavlutin.com/promise-all-settled/#21-all-promises-fulfilled) need to change const statuses = await productsPromise; to const statuses = await statusesPromise;

You're right! Fixed.

poplingue commented 2 years ago

Hello, Thanks, great article! What's the difference with Promise.all() ? Answer = «Promise.all() will reject immediately upon any of the input promises rejecting. In comparison, the promise returned by Promise.allSettled() will wait for all input promises to complete, regardless of whether or not one rejects. Consequently, it will always return the final result of every promise and function from the input iterable.»

marcodca commented 2 years ago

Somehow you always manage to right about stuff that is handy, but not mainstream, in a technical way but always understandable. To sum up: AMAZING content Dmitri.

panzerdp commented 2 years ago

Somehow you always manage to right about stuff that is handy, but not mainstream, in a technical way but always understandable. To sum up: AMAZING content Dmitri.

Thanks @marcodca!

masiucd commented 2 years ago

Great post Dimitri

tomfloresa commented 2 years ago

Dimitri, isn't allSettled a method instead of a function?

WORMSS commented 2 years ago

@tomfloresa I guess this comes down to the question of what data is it manipulating? Are statics considered methods if it doesn't get/set data on an singleton data source? I personally would argue it's a static function. Promise.allSettled() vs a method such as new Promise(...).then(...)

panzerdp commented 2 years ago

Dimitri, isn't allSettled a method instead of a function?

More exactly, it's a static method of Promise.

WORMSS commented 2 years ago

@panzerdp would love to hear your definition of what a method is vs function. I have always thought of it as methods sit on an instance of a class (or an object in JS terms) and gets atleast some of its data from the this or super.

Now as I said previously, this gets blurry around static methods backed by a singleton. But I don't believe all settled to be backed by one

panzerdp commented 2 years ago

@panzerdp would love to hear your definition of what a method is vs function. I have always thought of it as methods sit on an instance of a class (or an object in JS terms) and gets atleast some of its data from the this or super.

Now as I said previously, this gets blurry around static methods backed by a singleton. But I don't believe all settled to be backed by one

Check the difference between instance method and static method.

WORMSS commented 2 years ago

It seems there are massive debates online when I look for this. Some of the threads span multiple years and many dozens of users. So I think we will never come to an answer here.. From the gist of the conversations I seen elsewhere, it seems to fall mostly into 2 camps.

I'm gonna say piece of code rather than function/method

1) those who only consider how you call the piece of code 2) those who only consider if the piece of code has side effects.

1) how you call it

new Foo().bar(); // it's on a class instance, therefore a method
Foo.bar(); // it's on a class static, therefore a method
bar(); // not on a class, so it's a function

Regardless of what the piece of code does.

2) has side effects

// bar() { return 42; }
new Foo().bar(); // no side effects so it's a function
Foo.bar(); // no side effects so it's a function
bar(); // no side effects so it's a function

What ever hybrid use case I think up.

Whereas I am a bit of a mismash of the two, but I will admit, due to the crazy way my managers have shaped our codebase (some of which I agree with, some of which I am meh about) I am obviously biased by things I have encountered in my career.

Javascript is too wibbly-wobbly to be confined by those definitions.

We are currently using typescript abstract classes as a namespace and adding static pieces of code.. they are technically by (1) terminology a static method, but they are self contained pieces of code, (2) would say it's a function.

My mismatching comes with

new Foo().bar(); // is a method, regardless if it has side effects or not. Because yeah, that is how I roll.
Foo.bar(); // is a static function, because it doesn't have side effects or state

// baz() { return SomeStaticPropOrClosureVar.prop++; }
Foo.baz(); // is a static method because it has state;

Yes, very blurry, and requires you to know what is going on... but on the good side, it makes you know what is going on.. But since we are in Javascript land, we can always blur the lines even more..

const a = (() => {
  let state = 0;
  function b() {
    return state++;
  };
  return b;
})();

Is this a function? or a method? it is called by a(); but has state ?? Yeah.. quite blurry.. I honestly would prob flipflop with whatever my brain says first.. no strong feelings either way..

So, yeah, it's statics where I will switch between the two. But if it's on a class instance, I tend to thing of them as methods..

Though, knowing the way my mind works, I am sure I accidently call them functions all the time..