Skip to content

Ability to pause response on runtime in the worker #1109

Answered by kettanaito
rahulsinghk asked this question in Q&A
Discussion options

You must be logged in to vote

Hi, everyone! For those wondering how to pause/unpause a response with MSW, let me summarize and give a few suggestions below.

Basics

There's no concept of "pause" in HTTP responses because they are sent in one bulk. The exception being streams where you, indeed, can introduce an arbitrary pause between different streamed chunks. But let's talk about regular responses which is likely what you're mocking with MSW.

Using delay

The most straightforward way to "pause" a response is to pend a response via the ctx.delay() function.

rest.get('/resource', (req, res, ctx) => {
  // Delay this response by 3000ms.
  return res(ctx.delay(3000), ctx.text('Hello world!'))
})

Combine this with runtime h…

Replies: 4 comments 3 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
2 replies
@kettanaito
Comment options

@rahulsinghk
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@ArturDuziak
Comment options

Answer selected by kettanaito
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
feature needs:discussion scope:browser Related to MSW running in a browser
3 participants
Converted from issue

This discussion was converted from issue #1108 on February 19, 2022 11:56.