/* Code-Comments */

January 16, 2020

Mock API Call With Promised setTimeout

I was recently working on a project where I wanted a loading state while an API resolved.

Instead of testing with a live API, I wanted to be able to mock it.

An easy way to do that is to create a simple waitFor function that returns a Promise of a setTimeout. Then, await it.

For example:

const waitFor = ms => new Promise(r => setTimeout(r, ms))

const onClick = async event => {
  await waitFor(1000)
  //normal handling of event

Now, we can see what the UI does for 1000ms instead of actually making the call. And, if we want, we can use that time to freeze the UI by running the debugger.

Stephen Weiss

Thanks for reading! My name's Stephen Weiss. I live in Chicago with my wife, Kate, and dog, Finn.
Click here to see the archives of my weeks in review and sign up yourself!