R
Reactiflux

smoke3785 – 17-36 Jun 28

smoke3785 – 17-36 Jun 28

OROwen Rossi-Keen6/28/2023
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);
});
}
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!
SScriptyChris6/28/2023
function where it is called will not proceed until the timeout has elapsed
What do you mean? If i understand correctly, setTimeout rejects the promise, which is rather not the case you want to consider as positive Also, if resolve(true) is correctly called first and then setTImeout rejects promise, then you should use clearTimeout() when you call resolve(true) so timeout won't get called
const timeoutId = setTimeout(() => {
reject('Connection to Strapi timed out');
}, timeoutMs);

////
clearInterval(pingInterval);
clearTimeout(timeoutId)
resolve(true);
const timeoutId = setTimeout(() => {
reject('Connection to Strapi timed out');
}, timeoutMs);

////
clearInterval(pingInterval);
clearTimeout(timeoutId)
resolve(true);
@smoke3785
UUUnknown User6/30/2023
Message Not Public
Sign In & Join Server To View

Looking for more? Join the community!

R
Reactiflux

smoke3785 – 17-36 Jun 28

Join Server