Middle
// It is necessary to check the solution to the problem on two services by calling: // 1. checkResult(url1, solution) // 2. checkResult(url2, solution) // // checkResult: (url: string, solution: string | number) => Promise<boolean>; // // - If both requests return true - log success // - If at least one returns false - log fail // - If at least one does not respond - log error // - If at least one responds longer than 1 sec - log timeout
sobes.tech AI
Answer from AI
To solve the task, you need to call checkResult for two URLs in parallel and handle the results considering timeout and errors.
Example in JavaScript using Promise.race for timeout:
async function verifySolution(url1, url2, solution) {
const timeoutPromise = new Promise((_, reject) =>
setTimeout(() => reject(new Error('timeout')), 1000)
);
try {
const [res1, res2] = await Promise.all([
Promise.race([checkResult(url1, solution), timeoutPromise]),
Promise.race([checkResult(url2, solution), timeoutPromise])
]);
if (res1 === true && res2 === true) {
console.log('success');
} else {
console.log('fail');
}
} catch (error) {
if (error.message === 'timeout') {
console.log('timeout');
} else {
console.log('error');
}
}
}
Logic:
- If both responses are true — output "success".
- If at least one is false — output "fail".
- If at least one promise is rejected (e.g., network error) — output "error".
- If at least one promise does not respond within 1 second — output "timeout".