re: 9 Promising Promise Tips VIEW POST

VIEW FULL DISCUSSION

Nice. I may be in the 0.01% that needs to use .then(x, y) so thanks for explaining the difference

Also in your first example, I'm pretty sure

.then(r => {
    return serverStatusPromise(r); // this is a promise of { statusCode: 200 }
})
.then(

...can be simplified to

.then(serverStatusPromise)
.then(

... because of function currying, assuming ServerStatusPromise accepts one value and returns one value.

This means you can do things like:

fetch('https://example.com/')
.then(validateResponse)
.then(readResponseAsJSON)
.catch(handleResponseError)

I agree that that code can be simplified into the one you mentioned, but in my humble opinion:

  • I personally find it hard to follow what is going on when you simply pass a callback reference.
  • Using this style would make it difficult for new comers to actually understand what is going on.
  • It works fine for unary callbacks like in promises, but it is a source of cryptic bugs when there is an arity mismatch between the caller and the callback.
  • It also becomes a problem when you want pass method which is context (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)).
code of conduct - report abuse