Skip to content

Commit

Permalink
fix(from): objects that are thennable that happen to have a subscribe…
Browse files Browse the repository at this point in the history
… method will no longer error.
  • Loading branch information
benlesh committed Sep 28, 2020
1 parent f4cb146 commit 789d6e3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
17 changes: 15 additions & 2 deletions spec/observables/from-spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect } from 'chai';
import { TestScheduler } from 'rxjs/testing';
import { asyncScheduler, of, from, Observer, observable, Subject } from 'rxjs';
import { asyncScheduler, of, from, Observer, observable, Subject, noop } from 'rxjs';
import { first, concatMap, delay } from 'rxjs/operators';

// tslint:disable:no-any
Expand Down Expand Up @@ -149,7 +149,8 @@ describe('from', () => {
);
expect(nextInvoked).to.equal(false);
});
it(`should accept a function`, (done) => {

it(`should accept a function that implements [Symbol.observable]`, (done) => {
const subject = new Subject<any>();
const handler: any = (arg: any) => subject.next(arg);
handler[observable] = () => subject;
Expand All @@ -170,5 +171,17 @@ describe('from', () => {
);
handler('x');
});

it('should accept a thennable that happens to have a subscribe method', (done) => {
// There was an issue with our old `isPromise` check that caused this to fail
const input = Promise.resolve('test');
(input as any).subscribe = noop;
from(input).subscribe({
next: x => {
expect(x).to.equal('test');
done();
}
})
})
}
});
7 changes: 4 additions & 3 deletions src/internal/util/isPromise.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { isFunction } from "./isFunction";

/**
* Tests to see if the object is an ES2015 (ES6) Promise
* @see {@link https://www.ecma-international.org/ecma-262/6.0/#sec-promise-objects}
* Tests to see if the object is "thennable".
* @param value the object to test
*/
export function isPromise(value: any): value is PromiseLike<any> {
return !!value && typeof value.subscribe !== 'function' && typeof value.then === 'function';
return isFunction(value?.then);
}

0 comments on commit 789d6e3

Please sign in to comment.