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, Нурбек <[email protected]> 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
> [email protected]
> https://mail.mozilla.org/listinfo/es-discuss
>
>
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to