Strawman: `Function.prototype.try` (or `Function.try`)
kai zhu
kaizhu256 at gmail.com
Tue Aug 22 19:26:00 UTC 2017
> There exist lots of libraries that give this sort of functionality among others in the functional community -- lots of Either or Maybe implementations.
>
> I don't think this is a healthy addition to the spec since you would then want to consider all the other monadic goodies like mapping over these values, folding, "all" and "some", etc.
i agree. a 10-line vanilla es5 solution already exists for what i think are the expected use-cases.
1. the 10-line solution - function tryCatchOnError - https://kaizhu256.github.io/node-utility2/build..master..travis-ci.org/apidoc.html#apidoc.element.utility2.tryCatchOnError <https://kaizhu256.github.io/node-utility2/build..master..travis-ci.org/apidoc.html#apidoc.element.utility2.tryCatchOnError>
2. use-case #1 - https://kaizhu256.github.io/node-utility2/build..master..travis-ci.org/apidoc.html#apidoc.element.utility2.jwtA256GcmDecrypt <https://kaizhu256.github.io/node-utility2/build..master..travis-ci.org/apidoc.html#apidoc.element.utility2.jwtA256GcmDecrypt>
use tryCatchOnError to either
a) try to return the decrypted jwt token, or
b) return an empty object if an error was thrown during decryption
3. use-case #2 - https://kaizhu256.github.io/node-utility2/build..master..travis-ci.org/apidoc.html#apidoc.element.utility2.ajax <https://kaizhu256.github.io/node-utility2/build..master..travis-ci.org/apidoc.html#apidoc.element.utility2.ajax>
use tryCatchOnError in an asynchronous ajax function
4. use-case #3 - https://kaizhu256.github.io/node-utility2/build..master..travis-ci.org/coverage.html/node-utility2/test.js.html <https://kaizhu256.github.io/node-utility2/build..master..travis-ci.org/coverage.html/node-utility2/test.js.html>
use tryCatchOnError to test for exceptions
> On Aug 23, 2017, at 1:01 AM, Isiah Meadows <isiahmeadows at gmail.com> wrote:
>
> Usually, it's most convenient to just let errors easily fall through,
> using `try`/`catch`/`finally`. But when you're writing more low-level
> error-handling code, especially where errors are never exceptional
> (like test errors in a testing framework or task errors in a
> distributed worker pool implementation), it's often easier to handle
> completions/results as if they were values, like how Lua [1] and Rust
> [2] handle it.
>
> [1]: https://www.lua.org/pil/8.4.html
> [2]: https://doc.rust-lang.org/book/second-edition/ch09-02-recoverable-errors-with-result.html
>
> Here's my proposal: introduce either `Function.try(func, thisArg,
> ...args)` or `Function.prototype.try(thisArg, ...args)`. It works much
> like `Function.prototype.call`, except instead of returning/throwing,
> it always returns a `{thrown, value}` pair.
>
> Polyfilling either would be easy:
>
> ```js
> Function.try = (func, thisArg, ...args) => {
> try {
> return {thrown: false, value: func.call(thisArg, ...args)}
> } catch (e) {
> return {thrown: true, value: e}
> }
> }
>
> Function.prototype.try = function (thisArg, ...args) {
> try {
> return {thrown: false, value: this.call(thisArg, ...args)}
> } catch (e) {
> return {thrown: true, value: e}
> }
> }
> ```
>
> Engines could make this much more performant, though, by detecting the
> function, avoiding the object allocation if immediately destructured,
> optimizing the arguments like `Function.prototype.call`, and if called
> with a lambda, inlining the whole thing into a pseudo-`try`/`catch`.
>
> -----
>
> Isiah Meadows
> me at isiahmeadows.com
>
> Looking for web consulting? Or a new website?
> Send me an email and we can get started.
> www.isiahmeadows.com
> _______________________________________________
> 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/20170823/f2b693b0/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: PastedGraphic-3.png
Type: image/png
Size: 197146 bytes
Desc: not available
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20170823/f2b693b0/attachment-0004.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: PastedGraphic-6.png
Type: image/png
Size: 262337 bytes
Desc: not available
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20170823/f2b693b0/attachment-0005.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: PastedGraphic-4.png
Type: image/png
Size: 345095 bytes
Desc: not available
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20170823/f2b693b0/attachment-0006.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: PastedGraphic-7.png
Type: image/png
Size: 421450 bytes
Desc: not available
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20170823/f2b693b0/attachment-0007.png>
More information about the es-discuss
mailing list