refactor: improved transacion service somewhat (closes #22)
parent
62aa1a7d20
commit
90979e0631
@ -0,0 +1,34 @@ |
||||
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); |
||||
} |
||||
}); |
Loading…
Reference in new issue