Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bindCallback swallows error if it was thrown after the callback was executed, and the entire function is sycnhronous #3487

Closed
olessavluk opened this issue Mar 29, 2018 · 3 comments

Comments

@olessavluk
Copy link

olessavluk commented Mar 29, 2018

RxJS version:
5.5.8

Code to reproduce:
same for node and browser

const fn = (datum, cb) => { 
  cb(datum);  
  throw new Error("callback error");
};

Rx.Observable.bindCallback(fn)(42)
  .subscribe(
    v => console.log(v), 
    e => console.error(e),
    c => console.log("completed"),
  );

Expected behavior:
see 42 and then "callback error" in the console

Actual behavior:
console output:

42
completed

Additional information:

In the real world, I've captured this issue with ReactDOM.render method. If you run it with "bad" JSX it will call passed callback first and then throw an error.

I've tried to reproduce this case as a test, but it worked well in the test environment. So maybe there is something with test scheduler or production build

    it('should error if callback throws', () => {
      const expected = new Error('haha no callback for you');
      function callback(datum, cb) {
        cb(datum);
        throw expected;
      }
      const boundCallback = Observable.bindCallback(callback, null, rxTestScheduler);

      boundCallback(42)
        .subscribe((x: number) => {
          throw 'should not next';
        }, (err: any) => {
          expect(err).to.equal(expected);
        }, () => {
          throw 'should not complete';
        });

      rxTestScheduler.flush();
    });
@olessavluk olessavluk changed the title bindCallback swallow error, that was thrown after callback was executed bindCallback swallow error if it was thrown after the callback was executed Mar 29, 2018
@benlesh benlesh changed the title bindCallback swallow error if it was thrown after the callback was executed bindCallback swallows error if it was thrown after the callback was executed, and the entire function is sycnhronous Mar 29, 2018
@benlesh
Copy link
Member

benlesh commented Mar 29, 2018

I've updated the title of this issue, because it's specific to completely synchronous functions with callbacks.

@benlesh
Copy link
Member

benlesh commented Mar 29, 2018

In the short term, the fix for this is to pass a scheduler to the bindCallback function: bindCallback(fn, asapScheduler) for example.

@cartant
Copy link
Collaborator

cartant commented Sep 27, 2018

Closing this because it's addressed in #4089.

@cartant cartant closed this as completed Sep 27, 2018
@lock lock bot locked as resolved and limited conversation to collaborators Oct 27, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants