1

In JS, let's say I call a promise in a 'fire and forget' way. I don't care if it was successful or not, and I don't care what it returns as I want to return the same value anyways.

I can call

return myPromise().then(() => foo).catch(() => foo);

Is there a way to not repeat then then and catch parts?

What I've tried

I thought about using finally, thinking that it will take what finally returns (similar to what Java does by taking the return value of the finally block) : return myPromise().finally(() => foo); But it turns out it didn't do what I expected. According to mdn, this return value will be used to reject in case the original promise rejected.

Ricola
  • 2,621
  • 12
  • 22
  • 3
    `myPromise().catch(() => {}).then(() => foo)`? – deceze Jul 11 '22 at 10:42
  • @deceze Isn't that, apart from order, exactly the same as the OP's? Just wondering if I'm missing anything. For the OP: I don't think you will find something more succinct. – somethinghere Jul 11 '22 at 10:46
  • 1
    @somethinghere it's not exactly the same, since `foo` is only specified to be returned once, not in both the catch and then. – VLAZ Jul 11 '22 at 10:46
  • @VLAZ but it's almost as verbose and it's a little bit less explicit – Ricola Jul 11 '22 at 10:53
  • https://jsfiddle.net/eLuhc2xw/ – Thilina Koggalage Jul 11 '22 at 10:53
  • 3
    @Ricola You were trying to avoid the repetition of the `foo` expression, didn't you? That's exactly what deceze's code does. I'd argue it's even *more* explicit, since it spells out "*ignore any error, then always return `foo`*". – Bergi Jul 11 '22 at 11:49
  • True. This can be posted as an answer, I'll accept it. I was mainly looking for something more succinct but I don't think it's possible. – Ricola Jul 11 '22 at 12:54

3 Answers3

1

You can declare your result returning function once and then re-use it twice for the .then() and .catch() handlers

const myReturn = () => foo;

return myPromise()
    .then(myReturn)
    .catch(myReturn);

You can further shorten this by using both parameters of .then() which handle both the success and failure path:

const myReturn = () => foo;

return myPromise()
    .then(myReturn, myReturn);

If you find needing this a lot, you might make a small library function that takes a value and returns two functions that both just evaluate to it:

const doubleThunk = val =>
  [() => val, () => val];

Which would be used like this:

return myPromise()
    .then(...doubleThunk(foo));
VLAZ
  • 26,331
  • 9
  • 49
  • 67
1

From @deceze's comment and @Bergi clarification:

myPromise()
.catch(() => {}) // ignore any error 
.then(() => foo) // always return foo
Ricola
  • 2,621
  • 12
  • 22
0

You could have a look at Promise.allSettled if you're targeting newer browsers or Node.js versions.

This function will return a Promise that resolves whether or not the passed Promise resolves or rejects.

  
function foo(results) { 
    console.log('foo():', ...results)
}

// Success path
Promise.allSettled([Promise.resolve('Yay')]).then(foo)

// Failure path
Promise.allSettled([Promise.reject('Doh')]).then(foo)
.as-console-wrapper { max-height: 100% !important; }
Terry Lennox
  • 29,471
  • 5
  • 28
  • 40