Filtered Promise#catch

Michał Wadas michalwadas at gmail.com
Wed Oct 11 12:28:03 UTC 2017


I don't get why argument "it's easy to implement in user land" is raised.

All Array methods are easy to implement in user land. Including recent
additions like .includes and ES2015 methods.

Promises are easy to implement in user land. Smallest implementation is
less than 1kB AFAIR.

Maps, Sets, WeakSets are trivial to implement in user land if WeakMap is
present. Probably less than 50 lines of code.

Object.values, Object.entries, Object.assign,
Object.getOwnPropertyDescriptors, Object.is, Array.from, Array.of. These
methods were added recently but they can be implemented in user land.

On 11 Oct 2017 2:28 am, "Christopher Thorn" <morphcham at gmail.com> wrote:

> I agree that catch guards are useful, but they can be implemented in
> userland fairly ergonomically:
>
> ```
> function guard(predicate, callback) {
>   return function guarded(reason) {
>     if (!predicate(reason)) {
>       throw reason;
>     }
>     return callback(reason);
>   };
> }
>
> function instanceOf(constructor, callback) {
>   return guard(reason => reason instanceof constructor, callback);
> }
>
> Promise.resolve('invalid')
>   .then(JSON.parse)
>   .catch(instanceOf(SyntaxError, reason => {
>     // do something to handle the syntax error, or perhaps just silence it
>   }));
> ```
>
> Regards, Chris
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20171011/7fdb97cb/attachment.html>


More information about the es-discuss mailing list