mirror of
https://github.com/bitwarden/browser.git
synced 2024-10-09 05:57:40 +02:00
28 lines
613 B
TypeScript
28 lines
613 B
TypeScript
|
import { from, Observable, of, throwError } from "rxjs";
|
||
|
|
||
|
import { Utils } from "@bitwarden/common/misc/utils";
|
||
|
|
||
|
export type FunctionReturningAwaitable =
|
||
|
| (() => unknown)
|
||
|
| (() => Promise<unknown>)
|
||
|
| (() => Observable<unknown>);
|
||
|
|
||
|
export function functionToObservable(func: FunctionReturningAwaitable): Observable<unknown> {
|
||
|
let awaitable: unknown;
|
||
|
try {
|
||
|
awaitable = func();
|
||
|
} catch (error) {
|
||
|
return throwError(() => error);
|
||
|
}
|
||
|
|
||
|
if (Utils.isPromise(awaitable)) {
|
||
|
return from(awaitable);
|
||
|
}
|
||
|
|
||
|
if (awaitable instanceof Observable) {
|
||
|
return awaitable;
|
||
|
}
|
||
|
|
||
|
return of(awaitable);
|
||
|
}
|