storybook-addon-mock icon indicating copy to clipboard operation
storybook-addon-mock copied to clipboard

Support async response function

Open rochejul opened this issue 2 years ago • 1 comments

Hi

We could define a function when we want to return a custom response.

It should be great if it supports async response. Especially if we want to call a mock server.

It could be evolved like so:

setTimeout(async () => {
    let payload = response;

    if (typeof response === 'function') {
        payload = response(request);

        if (response.constructor.name === 'AsyncFunction' || payload instanceof Promise) {
            payload = await payload;
        }
    }

    resolve(new Response(url, status, payload));
}, +delay);

Thoughts?

rochejul avatar Dec 15 '22 12:12 rochejul

I think this is a necessary addition. My use case is very simple - I want to read the JSON send by the client and create a response based on that. This is sadly not possible, as Request.json() returns a promise. Any workarounds for this?

rChaoz avatar Apr 16 '24 21:04 rChaoz