I’d much rather see something more like C# 7’s throw expressions: 
https://docs.microsoft.com/en-us/dotnet/articles/csharp/whats-new/csharp-7#throw-expressions

```js
// original example
fs.writeFile("message.txt", "Hello Node.js", err => err ? throw err : 
console.log("The file has been saved.");

// better example
function choose(kind) {
  return kind === 0 ? choseZero() : kind === 1 ? chooseOne() : throw new 
Error("out of range");
}
```

Ron

From: es-discuss [mailto:[email protected]] On Behalf Of T.J. 
Crowder
Sent: Tuesday, April 11, 2017 5:20 AM
To: Elie Rotenberg <[email protected]>
Cc: Нурбек <[email protected]>; es-discuss <[email protected]>
Subject: Re: throwif operator

I don't think we need a new keyword for this, both because of Elie's point 
about handling it with a `throwif` function, and because Node's callback 
pattern is a bit old-fashioned in the world of promises and async/await. Until 
the Node API is updated to support promises natively, you can use one of the 
various "promisifying" libs to promisify the API:

```js
const pfs = /*...promisified version of fs*/;
```

...and then:

```js
pfs.writeFile("message.txt", "Hello Node.js")
    .then(() => console.log("The file has been saved."));
```

...and like your original code snippet, it will (on recent versions) terminate 
Node if you don't handle the error.

Combined with `async`/`await`, it becomes even cleaner:

```js
await pfs.writeFile("message.txt", "Hello Node.js");
console.log("The file has been saved.");
```

(Of course, that has to be within an `async` function.)

-- T.J. Crowder

On Tue, Apr 11, 2017 at 1:15 PM, Elie Rotenberg 
<[email protected]<mailto:[email protected]>> wrote:
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]<mailto:[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]<mailto:[email protected]>
https://mail.mozilla.org/listinfo/es-discuss<https://na01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fmail.mozilla.org%2Flistinfo%2Fes-discuss&data=02%7C01%7Cron.buckton%40microsoft.com%7Cf2cf33458e3a466e917708d480d52f9b%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C636275100507832377&sdata=BZKPMSUjyLwQJr8fNltJnVFt4%2F8wLcLqHop92DUoW1U%3D&reserved=0>


_______________________________________________
es-discuss mailing list
[email protected]<mailto:[email protected]>
https://mail.mozilla.org/listinfo/es-discuss<https://na01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fmail.mozilla.org%2Flistinfo%2Fes-discuss&data=02%7C01%7Cron.buckton%40microsoft.com%7Cf2cf33458e3a466e917708d480d52f9b%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C636275100507832377&sdata=BZKPMSUjyLwQJr8fNltJnVFt4%2F8wLcLqHop92DUoW1U%3D&reserved=0>

_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to