Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to add a delay for a specific response? #232

Open
freearhey opened this issue Dec 14, 2019 · 5 comments
Open

Is it possible to add a delay for a specific response? #232

freearhey opened this issue Dec 14, 2019 · 5 comments

Comments

@freearhey
Copy link

I mean something like this:

mock.onPost('/any').reply(201, null, { delay: 2000 })
@ctimmerm
Copy link
Owner

Since you can reply with a promise, you can use that in combination with setTimeout to have per-request delays:

mock.onGet('/any').reply(function(config) {
  return new Promise(function(resolve, reject) {
    setTimeout(function() {
        resolve([201, null]);
    }, 2000);
  });
});

If it's something you need to do often, you could write a helper function for it:

const withDelay = (delay, response) => config => {
  return new Promise(function(resolve, reject) {
    setTimeout(function() {
        resolve(response);
    }, delay);
  });
});

// And then use it:
mock.onGet('/any').reply(withDelay(2000, [201, null]));

@freearhey
Copy link
Author

@ctimmerm Thanks for the tip! But don't you think it would be better to add this option to the adapter itself?

@iomariani
Copy link

It would definitely be nice if we could do something like this:

mock.onGet('/any').delay(1000).reply((config) => {
  return [200];
});

So we don't have to wrap the entire response function with a Promise...

prashanth-92 added a commit to prashanth-92/axios-mock-adapter that referenced this issue Sep 6, 2021
prashanth-92 added a commit to prashanth-92/axios-mock-adapter that referenced this issue Sep 6, 2021
@prashanth-92
Copy link
Contributor

@ctimmerm An attempt for a helper function for the delay: #312 Thoughts?

marcbachmann pushed a commit that referenced this issue Sep 11, 2023
marcbachmann pushed a commit that referenced this issue Sep 11, 2023
@seriouslag
Copy link

I opened a PR to add types to replyWithDelay

#383

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants