Skip to content

Commit 5178ab9

Browse files
committed
feat(forEach): deprecating passing promise constructor
- Adds documentation - Deprecates promise constructor
1 parent 8b74c2a commit 5178ab9

File tree

1 file changed

+86
-1
lines changed

1 file changed

+86
-1
lines changed

src/internal/Observable.ts

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,10 +231,95 @@ export class Observable<T> implements Subscribable<T> {
231231
}
232232
}
233233

234+
/**
235+
* Subscribes to and nexts out each value of from the observable, passing values
236+
* to the supplied next handler. Useful for async-await
237+
*
238+
* ### Example
239+
*
240+
* ```javascript
241+
* async function foo() {
242+
* console.log('start logging');
243+
* await interval(1000).pipe(take(3))
244+
* .forEach(x => console.log(x));
245+
* console.log('done logging');
246+
* }
247+
*
248+
* foo();
249+
*
250+
* // Logs
251+
* // "start logging"
252+
* // 0
253+
* // 1
254+
* // 2
255+
* // "done logging"
256+
* ```
257+
* @param next a handler for each value emitted by the observable
258+
* @returns A promise that resolves when the observable completes or rejects if the
259+
* observable errors.
260+
*/
261+
forEach(next: (value: T) => void): Promise<void>;
262+
263+
/**
264+
* Subscribes to and nexts out each value of from the observable, passing values
265+
* to the supplied next handler. The subscription may be cancelled with a provided
266+
* subscription. When the provided subscription is unsubscribed, the operation is cancelled
267+
* and the returned promise will reject with "Observable forEach unsubscribed" so that
268+
* it can be handled in a try-catch block in async-await.
269+
*
270+
* ### Example
271+
*
272+
* ```js
273+
* async function foo() {
274+
*
275+
* async function foo() {
276+
* console.log('start logging');
277+
*
278+
* const cancel = new Subscription();
279+
* setTimeout(() => cancel.unsubscribe(), 2500);
280+
*
281+
* try {
282+
* await interval(1000).pipe(take(3))
283+
* .forEach(x => console.log(x), cancel);
284+
* } catch (err) {
285+
* console.log("ERROR: " + err.message);
286+
* }
287+
*
288+
* console.log('done logging');
289+
* }
290+
*
291+
* foo();
292+
*
293+
* // Logs
294+
* // "start logging"
295+
* // 0
296+
* // 1
297+
* // "ERROR: Observable forEach unsubscribed"
298+
* // "done logging"
299+
* }
300+
*
301+
* ```
302+
*
303+
* @param next a handler for each value emitted by the observable
304+
* @param cancelSubs a {@link Subscription} used like a cancellation token, that,
305+
* when unsubscribed, will reject the returned promise with an error
306+
* @returns A promise that resolves when the observable completes or rejects if the
307+
* observable errors or is unsubscribed.
308+
*/
309+
forEach(next: (value: T) => void, cancelSubs: Subscription): Promise<void>;
310+
311+
/**
312+
* Nexts out each value from the observable to the `next` handler, and returns a promise,
313+
* created from a specified promise constructor that resolves on Observable completion, or
314+
* rejects if there's an error.
315+
* @deprecated use {@link config} to configure the Promise Constructor if you need to configure promises.
316+
*/
317+
forEach(next: (value: T) => void, promiseCtor: PromiseConstructorLike): Promise<void>;
318+
234319
/**
235320
* @method forEach
236321
* @param {Function} next a handler for each value emitted by the observable
237-
* @param {PromiseConstructor} [promiseCtor] a constructor function used to instantiate the Promise
322+
* @param {PromiseConstructorLike|Subscription} [promiseCtorOrSubscription] a constructor function used to instantiate the Promise
238323
* @return {Promise} a promise that either resolves on observable completion or
239324
* rejects with the handled error
240325
*/

0 commit comments

Comments
 (0)