refactor: improved transacion service somewhat (closes #22) #24
Merged
inga-lovinde
merged 1 commits from refactor-22-simplify-transaction-service
into main
11 months ago
@ -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