A Result class for functions that may return errors

Oriol Bugzilla oriol-bugzilla at hotmail.com
Tue Oct 18 19:06:15 UTC 2016


Not sure if I'm missing something, but wouldn't it be trivial to code that constructor in JS?

```js
class Result {
  constructor (type, value=true) {
    this[type] = value;
  }
}
function add(data) {
  if (data !== Object(data)) {
    return new Result('error', new Error('The data is not an object.'));
  }
  return new Result('success');
}
console.log(add({}).success); // true
console.log(add(12).error.message); // 'The data is not an object.'
```

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20161018/62849a22/attachment.html>


More information about the es-discuss mailing list