Promise.any First Success
JavaScript
Medium
3 views
Problem Description
Given n numbers, a task succeeds if number is divisible by 5. Run tasks and print the first successful number in input order. If none, print ALL_FAIL.
Input Format
Line1: n. Line2: n integers.
Output Format
Number or ALL_FAIL.
Official Solution
const fs=require('fs');const raw=fs.readFileSync(0,'utf8').trim();if(!raw)process.exit(0);const a=raw.split(/\\s+/).map(Number);let i=0;const n=a[i++];const arr=a.slice(i,i+n);const task=x=>Promise.resolve().then(()=>{if(x%5===0)return x;throw new Error('NO');});(async()=>{try{const v=await Promise.any(arr.map(task));process.stdout.write(String(v));}catch(e){process.stdout.write('ALL_FAIL');}})();
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!