Timeout mechanism using Promise.race

Let's build on top of simple one-liner I've written about previously that allows you to pause code execution. Here it is one more time:

const pause = time => new Promise(resolve => setTimeout(resolve, time))

Simply combine it with Promise.race function whenever you need to ensure that code continues to execute after a specified amount of time. Like this:

const data = await Promise.race([fetchOverSlowNetwork(), pause(5000)]);

This will give fetchOverSlowNetwork a chance to resolve in 5 seconds. Otherwise the code continues to run with data set to undefined.