Skip to content

Instantly share code, notes, and snippets.

@mkulke
Created May 8, 2018 13:37
Show Gist options
  • Save mkulke/a216ed37f84e465d258241a1c7285add to your computer and use it in GitHub Desktop.
Save mkulke/a216ed37f84e465d258241a1c7285add to your computer and use it in GitHub Desktop.
RxJS operators
import { Observable } from 'rxjs/Rx';
type Tuple<T, U> = [T, U];
/**
* Operator which for values of a promise to be concated to an emission of the observable. For each
* emission, the result of the promise will be triggered and appended into an array with the
* emitted value, which can be used in the next emission block. Returns a lambda that can be used
* within `let`, for example.
*
* @param {Promise<U>} - A promise that returns a certain value.
*
* @return {Function} - A lambda that receives an observable and outputs the desired observable
* which emits values from the base observable and the promise itself.
*/
function waitFor<T, U>(
promise: Promise<U>,
): (o$: Observable<T>) => Observable<Tuple<T, U>> {
return (src$: Observable<T>) =>
src$.mergeMap((val: T): Promise<Tuple<T, U>> =>
promise.then((promiseVal: U) => {
const tuple: Tuple<T, U> = [val, promiseVal];
return tuple;
}),
);
}
export { waitFor };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment