sodiray / radash

Functional utility library - modern, simple, typed, powerful
https://radash-docs.vercel.app
MIT License
4.18k stars 167 forks source link

tryit method doesn't works with a class #338

Open fontanaen opened 1 year ago

fontanaen commented 1 year ago

I'm using extended class in my code and I found that this use case doesn't work. I can only call method passed as parameter, if my method call methods from the current or extended object it fails

import { tryit } from 'radash';

class Foo {
  protected foo() {
    return 'foo';
  }
}

class Bar extends Foo {
  bar() {
    return this.foo();
  }
}

const work = () => {
  console.log(new Bar().bar());
};

const fail = () => {
  const [err, result] = tryit(new Bar().bar)();
  if (err) throw err;
};

work(); // foo
fail(); // Error: Cannot read properties of undefined (reading 'foo')
suchangv commented 5 months ago

It's not redash problem, change your code like this.

class Bar extends Foo {
  bar = () => {
    return this.foo();
  }
}

Btw, javascript's this keyword is really bad!