Owen Rossi-Keen
Owen Rossi-Keen14mo ago

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);
});
}
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!
2 Replies
ScriptyChris
ScriptyChris14mo ago
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
reactibot
reactibot14mo ago
This thread hasn’t had any activity in 36 hours, so it’s now locked. Threads are closed automatically after 36 hours. If you have a followup question, you may want to reply to this thread so other members know they're related. https://discord.com/channels/102860784329052160/565213527673929729/1123668289357352961