Quantcast

throwif operator

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

throwif operator

Нурбек
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
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: throwif operator

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



_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: throwif operator

T.J. Crowder-2
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 <[hidden email]> 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, Нурбек <[hidden email]> 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
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss



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



_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: throwif operator

Ron Buckton

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:[hidden email]] On Behalf Of T.J. Crowder
Sent: Tuesday, April 11, 2017 5:20 AM
To: Elie Rotenberg <[hidden email]>
Cc: Нурбек <[hidden email]>; es-discuss <[hidden email]>
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 <[hidden email]> 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, Нурбек <[hidden email]> 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
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss

 


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

 


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: throwif operator

T.J. Crowder-2
I'd like `throw` expressions (a lot). I wouldn't use them for this, but I want them for many of the same reasons outlined in the C# case for them.

-- T.J. Crowder


On Wed, Apr 12, 2017 at 1:10 AM, Ron Buckton <[hidden email]> wrote:

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:[hidden email]] On Behalf Of T.J. Crowder
Sent: Tuesday, April 11, 2017 5:20 AM
To: Elie Rotenberg <[hidden email]>
Cc: Нурбек <[hidden email]>; es-discuss <[hidden email]>
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 <[hidden email]> 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, Нурбек <[hidden email]> 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
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss

 


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

 



_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Re: throwif operator

Нурбек
In reply to this post by Нурбек
Yes, throw expressions would be helpful to shorten if statements to one-liner ternary operators. I agree with you.

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Re: throwif operator

Isiah Meadows-2
I like the idea of throw expressions a lot, too. One area where it'd
be useful would be in arrow functions - I really don't enjoy the extra
4-5 characters just to throw an unconditional error from an arrow
function.

```js
// Done this a lot:
foo(...args, () => { throw new Error() })

// Much nicer:
foo(...args, () => throw new Error())
```
-----

Isiah Meadows
[hidden email]


On Wed, Apr 12, 2017 at 3:11 AM, Нурбек <[hidden email]> wrote:
> Yes, throw expressions would be helpful to shorten if statements to
> one-liner ternary operators. I agree with you.
>
> _______________________________________________
> es-discuss mailing list
> [hidden email]
> https://mail.mozilla.org/listinfo/es-discuss
>
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Loading...