setTimeout() in await axios get request

Issue

I want my await axios.get() to be delayed. How can I incorporate a setTimeout in my function?

export function functionName() {

  return useMutation(
    async filter => {
      const {data} = await axios.post<Core.Paths.CreateResponse.RequestBody, AxiosResponse<string>>(
        "/path/api/response", filter
      );
      const {data: blob} = await axios.get(`/path/api/document/${data}`, {responseType: "blob"});
      return URL.createObjectURL(blob);
    },
    }
  );
}

Solution

You can introduce a delay via Promise.

const delay = () => {
  return new Promise(resolve => setTimeout(resolve, 2000));
};

const mutation = async (name) => {
  console.log(`Ran: ${name}`);
};

const someFuction = async () => {
  await mutation('mutation one');

  await delay();

  await mutation('mutation two');
};

someFuction();

Answered By – rightdroid

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published