( promise: Promise<T>, time: number, fn: (res: TWithTimeoutNotifyResult<T>) => any )
| 35 | err: undefined | Error; |
| 36 | }; |
| 37 | export const withTimeoutNotify = <T>( |
| 38 | promise: Promise<T>, |
| 39 | time: number, |
| 40 | fn: (res: TWithTimeoutNotifyResult<T>) => any |
| 41 | ) => { |
| 42 | const res: TWithTimeoutNotifyResult<T> = { timeouted: false, result: undefined, settled: false, err: undefined }; |
| 43 | const cid = setTimeout(() => { |
| 44 | res.timeouted = true; |
| 45 | fn(res); |
| 46 | }, time); |
| 47 | return promise |
| 48 | .then((result: T) => { |
| 49 | clearTimeout(cid); |
| 50 | res.result = result; |
| 51 | res.settled = true; |
| 52 | }) |
| 53 | .catch((e) => { |
| 54 | clearTimeout(cid); |
| 55 | res.err = e; |
| 56 | res.settled = true; |
| 57 | }) |
| 58 | .then(() => { |
| 59 | fn(res); |
| 60 | return res; |
| 61 | }); |
| 62 | }; |
no outgoing calls
no test coverage detected