Timeout Simulation
JavaScript
Medium
4 views
Problem Description
Two integers workMs and timeoutMs are given. Simulate a task that resolves after workMs. If it does not finish within timeoutMs, print TIMEOUT else DONE.
Input Format
One line: workMs timeoutMs.
Output Format
TIMEOUT or DONE.
Official Solution
const fs=require('fs');const [work,limit]=fs.readFileSync(0,'utf8').trim().split(/\\s+/).map(Number);const workP=new Promise(res=>setTimeout(()=>res('DONE'),work));const timeP=new Promise((_,rej)=>setTimeout(()=>rej(new Error('TIMEOUT')),limit));(async()=>{try{const v=await Promise.race([workP,timeP]);process.stdout.write(String(v));}catch(e){process.stdout.write(e.message);} })();
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!