panzerdp / dmitripavlutin.com-comments

7 stars 0 forks source link

what-is-javascript-promise/ #131

Open utterances-bot opened 2 years ago

utterances-bot commented 2 years ago

What is a Promise in JavaScript?

The post that I had wanted to read myself to understand promises.

https://dmitripavlutin.com/what-is-javascript-promise/

blessedneelesh commented 2 years ago

Marvellous

salesh commented 2 years ago

As always nice post Dmitri!

kausar128 commented 2 years ago

Wonderful simplification.

popiang commented 2 years ago

Thanks for the very good explanation..:)

panzerdp commented 2 years ago

As always nice post Dmitri!

Thanks @salesh!

panzerdp commented 2 years ago

Thanks for the very good explanation..:)

You're welcome @popiang!

ohjuny commented 2 years ago

Thank you for another fantastic article! Just wondering if anyone has an answer to the "challenge" at the bottom of the article?

icanhazsoulz commented 2 years ago

promise.then(fn1, fn2) promise.then(fn1).catch(fn2)

The second variant will process a reject result of fn1, if any, while the first one won't.

pboto commented 2 years ago

Very clear and very well written, Dimitri. Thanks a lot.

panzerdp commented 2 years ago

Very clear and very well written, Dimitri. Thanks a lot.

You're welcome Paulo @pboto!

AlexandreSherozia commented 2 years ago

Thanks a lot Dimitri, for this great explanation!

panzerdp commented 2 years ago

Thanks a lot Dimitri, for this great explanation!

@AlexandreSherozia Glad you like it Alexandre!

shivam3596 commented 2 years ago

4.1 await-ing promise value

resolve has missing closing ')' bracket

function getList() {
  return new Promise(resolve => {
    setTimeout(() => resolve(['Joker', 'Batman'], 1000);
  });
}

it should be

function getList() {
  return new Promise(resolve => {
    setTimeout(() => resolve(['Joker', 'Batman']), 1000);
  });
}
WORMSS commented 2 years ago

Just as an alternative to times you want to 'fake' a delay... you can do a syntax like this

function getList() {
  return new Promise(resolve => setTimeout(resolve, 1000, ['Joker', 'Batman']));
}

Any arguments passed into setTimout after the duration becomes arguments for the settimeout callback

panzerdp commented 2 years ago

Just as an alternative to times you want to 'fake' a delay... you can do a syntax like this

function getList() {
  return new Promise(resolve => setTimeout(resolve, 1000, ['Joker', 'Batman']));
}

Any arguments passed into setTimout after the duration becomes arguments for the settimeout callback

Good idea @WORMSS! Thanks for sharing.

panzerdp commented 2 years ago

4.1 await-ing promise value resolve has missing closing ')' bracket

Thanks @shivam3596. Fixed.

annasudol commented 2 years ago

amazing !

panzerdp commented 1 year ago

@annasudol Thanks!