Owen Rossi-KeenO
Reactiflux3y ago
4 replies
Owen Rossi-Keen

smoke3785 – 17-36 Jun 28

This function is meant to 'wake up` the strapi server and give it a minute to wake up before proceeding with the build:
async function ensureStrapiConnection(timeoutSeconds = 60) {
  let timeoutMs = timeoutSeconds * 1000;
  let request = 0;

  return await new Promise(async (resolve, reject) => {
    setTimeout(() => {
      reject('Connection to Strapi timed out');
    }, timeoutMs);

    let pingInterval = setInterval(async () => {
      request++;

      const result = await axios.get(process.env.STRAPI_API_ENDPOINT, {
        headers: {
          Authorization: 'Bearer ${process.env.STRAPI_AUTHENTICATION_CODE}',
        },
      });

      if (result?.status === 200) {
        clearInterval(pingInterval);
        resolve(true);
        return;
      }
    }, 2000);
  });
} 

Unfortunately, the function where it is called will not proceed until the timeout has elapsed, although it will console.log(true) as soon as it's received a positive result. Any idea where I've gone wrong? Thanks!
Was this page helpful?