1

Since there is a new Promise combinator called Promise.allSettled I am interested in performing some code logic based on the status of the resolution of Promises.

eg:

Promise.allSettled([
    callApi("http://example.com/wishlist"),
    callApi("http://example.com/brands")
])
.then(([wishlist, brands]) => {
    if(brands.status === "failed"){
        notifyMe()
    }
})

As you can see I am using a static value to compare the resolution status of promises brands.status === "failed"

You can call me paranoid, but how JavaScript is evolving this value might change in the future and I would prefer to have something less static.

My question is: Is there any Symbol.PromiseRejected|Symbol.PromiseResolved or something relevant that encapsulates the promise resolution ?

Rosmarine Popcorn
  • 10,761
  • 11
  • 59
  • 89

1 Answers1

1

You can call me paranoid, but how JavaScript is evolving this value might change in the future

No, it won't. JS will (need to) stay backwards-compatible, this will never change. There's no reason to rename the .status property either, it's not like the property names of result objects like this (or iterator result objects etc) would collide with anything new.

My question is: Is there any Symbol.PromiseRejected|Symbol.PromiseResolved or something relevant that encapsulates the promise resolution?

No, there's not. They're not symbols anyway.

If you're absolutely paranoid, you can either write your own trivial allSettled function where you can control the shape of the result objects, or you can write isRejected(result) and isFulfilled(result) helper functions to call everywhere so that in the unlikely event of something changing you would need to change only a single line of code.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375