throwif operator

Elie Rotenberg elie at rotenberg.io
Tue Apr 11 12:15:11 UTC 2017


const throwIf = (fn) => (err, ...args) => {
  if(err) throw err;
  return fn(...args);
};

...

fs.writeFile('message.txt', 'Hello Node.js', throwIf(() => console.log('The
file has been saved')));


No need to extend the language and preempt keywords with stuff you can very
easily implement in userland.
I'm not even convinced such a construct would be a good idea anyway to
encourage rethrowing errors without thinking if it can be handled locally.

On Tue, Apr 11, 2017 at 2:06 PM, Нурбек <nurbek.ab at gmail.com> wrote:

> An example from node.js documentation:
>
> fs.writeFile('message.txt', 'Hello Node.js', (err) => {
>   if (err) throw err;
>   console.log('The file has been saved!');
> });
>
> This looks like a common way to handle errors when the first parameter is
> an instance of Error.
>
> Yes, this line of code is short and easy to copy-paste.
> But introducing something like this:
>
> fs.writeFile('message.txt', 'Hello Node.js', (err) => {
>   throwif err;
>   console.log('The file has been saved!');
> });
>
> would make the code much cleaner and bring some sort of standart way to
> handle such errors.
>
> _______________________________________________
> 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/20170411/404dad83/attachment.html>


More information about the es-discuss mailing list