Skip to content

Commit

Permalink
fixup! feat(forEach): now allows cancellation by passing a Subscription
Browse files Browse the repository at this point in the history
  • Loading branch information
benlesh committed Jul 30, 2018
1 parent 3437239 commit cb6dd81
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/internal/Observable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,12 +325,16 @@ export class Observable<T> implements Subscribable<T> {
*/
forEach(next: (value: T) => void, promiseCtorOrSubscription?: PromiseConstructorLike|Subscription): Promise<void> {
let promiseCtor: PromiseConstructorLike;
let subs = isSubscription(promiseCtorOrSubscription) ? promiseCtorOrSubscription : undefined;
let subs: Subscription;
if (isSubscription(promiseCtorOrSubscription)) {
subs = promiseCtorOrSubscription;
} else {
subs = new Subscription();
promiseCtor = promiseCtorOrSubscription;
}
promiseCtor = getPromiseCtor(promiseCtor);

return new promiseCtor<void>((resolve, reject) => {
subs = subs || new Subscription();

// If the promise resolves with a complete, calling reject should noop.
subs.add(() => reject(new Error('Observable forEach unsubscribed')));

Expand Down

0 comments on commit cb6dd81

Please sign in to comment.