mswMock Service Worker
  1. Api
  2. context
  3. delay()

delay()

Delays the response by the given duration (in ms). When no duration is provided, uses a random realistic server response time.

Realistic response time is a random duration based on the average response time of a real server, considering a good connection.

Call signature

1function delay(duration?: number): MockedResponse

Examples

Explicit response delay

1rest.post('/login', (req, res, ctx) => {
2 return res(
3 // Delays response for 2000ms.
4 ctx.delay(2000),
5 ctx.json({
6 id: 'abc-123',
7 }),
8 )
9})

Implicit response delay

When no delay duration is explicitly provided, Mock Service Worker uses a random realistic server response time on the particular mocked response.

1rest.delete('/post/:postId', (req, res, ctx) => {
2 return res(
3 ctx.delay(),
4 ctx.json({
5 message: `Post ${req.params.postId} successfully deleted!`,
6 }),
7 )
8})