typicode / json-server

Get a full fake REST API with zero coding in less than 30 seconds (seriously)
Other
72.93k stars 7.02k forks source link

Is there anyway to delay the response time? #647

Open toe-lai opened 7 years ago

toe-lai commented 7 years ago

I would like to delay the response time about 30s for testing loading case. Is it possible?

djhojd commented 7 years ago

Check CLI usage, you can add --delay [ms], in your case either start with json-server --delay 30000 or create a file called json-server.json and add that option there.

159159951 commented 6 years ago

Hi @toe-lai, Your question is resolved before in this link: https://github.com/typicode/json-server/issues/188

Aliyamash commented 9 months ago

how can i add it in json-server.json ?

Rockheung commented 9 months ago

Please give us back --delay option.

piotr-jura-udemy commented 9 months ago

Not sure why delay was removed. "use Chrome's Network tab > throttling to delay requests instead of --delay CLI option". What if I used json-server in Next 14 app dir, where I fetch data on the server? How I'm supposed to enable throttling there?

typicode commented 9 months ago

You can do it like this:

const sleep = (ms = 0) => new Promise(resolve => setTimeout(resolve, ms));

await sleep(5000)
await fetch(...)

And if you're using something like axios, probably with an interceptor. There's more control over which request should have a delay or not.

On macOS, you have Network Link Conditioner as well.

nashashibi commented 9 months ago

i've raised this PR.

meanwhile, I made the same changes locally in the globally installed package and is working fine.

You can check the PR and do the same if you don't wanna wait 🤓