Merge pull request 'refactor: improved transacion service somewhat (closes #22)' (#24) from refactor-22-simplify-transaction-service into main
Reviewed-on: #24main
commit
e59ff2c202
@ -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