You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
test-assignment-payments/src/shared/reactive.ts

34 lines
804 B

import type { Observable } from 'rxjs';
export const promisifyObservable =
<TArgs extends any[], TOutput>(f: (...args: TArgs) => Observable<TOutput>) =>
(...args: TArgs) =>
new Promise<TOutput>((_resolve, _reject) => {
let isCalled = false;
const resolve = (result: TOutput) => {
if (isCalled) {
console.log('observable emits more than once');
return;
}
_resolve(result);
};
const reject = (error: unknown) => {
if (isCalled) {
console.log('observable emits more than once');
return;
}
_reject(error);
};
try {
f(...args).subscribe({
next: resolve,
error: reject,
});
} catch (error) {
reject(error);
}
});