| 43 | * but is not async internally |
| 44 | */ |
| 45 | export class SyncPromise<T> implements PromiseLike<T> { |
| 46 | private _state: State; |
| 47 | private _handlers: Array<[boolean, (value: T) => void, (reason: any) => any]>; |
| 48 | private _value: any; |
| 49 | |
| 50 | public constructor(executor: Executor<T>) { |
| 51 | this._state = STATE_PENDING; |
| 52 | this._handlers = []; |
| 53 | |
| 54 | this._runExecutor(executor); |
| 55 | } |
| 56 | |
| 57 | /** @inheritdoc */ |
| 58 | public then<TResult1 = T, TResult2 = never>( |
| 59 | onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | null, |
| 60 | onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null, |
| 61 | ): PromiseLike<TResult1 | TResult2> { |
| 62 | return new SyncPromise((resolve, reject) => { |
| 63 | this._handlers.push([ |
| 64 | false, |
| 65 | result => { |
| 66 | if (!onfulfilled) { |
| 67 | // TODO: ¯\_(ツ)_/¯ |
| 68 | // TODO: FIXME |
| 69 | resolve(result as any); |
| 70 | } else { |
| 71 | try { |
| 72 | resolve(onfulfilled(result)); |
| 73 | } catch (e) { |
| 74 | reject(e); |
| 75 | } |
| 76 | } |
| 77 | }, |
| 78 | reason => { |
| 79 | if (!onrejected) { |
| 80 | reject(reason); |
| 81 | } else { |
| 82 | try { |
| 83 | resolve(onrejected(reason)); |
| 84 | } catch (e) { |
| 85 | reject(e); |
| 86 | } |
| 87 | } |
| 88 | }, |
| 89 | ]); |
| 90 | this._executeHandlers(); |
| 91 | }); |
| 92 | } |
| 93 | |
| 94 | /** @inheritdoc */ |
| 95 | public catch<TResult = never>( |
| 96 | onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | null, |
| 97 | ): PromiseLike<T | TResult> { |
| 98 | return this.then(val => val, onrejected); |
| 99 | } |
| 100 | |
| 101 | /** @inheritdoc */ |
| 102 | public finally<TResult>(onfinally?: (() => void) | null): PromiseLike<TResult> { |
nothing calls this directly
no outgoing calls
no test coverage detected