Implement promiseTimeout(promise, ms). It returns a new promise. If promise settles within ms milliseconds, the new promise settles the same way. Settles means resolves or rejects.
- If
promiseresolves or rejects first, pass on its value or reason. - If
msms pass first, reject with anErrorwhose message is exactlyTimed out after <ms> ms. For example:Timed out after 100 ms. A late result frompromiseis then ignored. - When
promisesettles in time, clear the timer so nothing is left pending.
Input: await promiseTimeout(wait(50, 'ok'), 100)
Output: 'ok'
Input: await promiseTimeout(wait(500, 'ok'), 100)
Output: rejects with Error('Timed out after 100 ms') at 100 ms