Promise Reject Catch (Single)
JavaScript
Easy
2 views
Problem Description
One integer n is given. Create a promise that rejects if n is odd, else resolves. Using async/await and try/catch, print OK or ERR.
Input Format
One integer n.
Constraints
n is integer.
Official Solution
const fs=require('fs');const s=fs.readFileSync(0,'utf8').trim();if(!s)process.exit(0);const n=Number(s);const task=()=>new Promise((res,rej)=>{if(n%2===0)res('ok');else rej(new Error('ODD'));});(async()=>{try{await task();process.stdout.write('OK');}catch(e){process.stdout.write('ERR');}})();
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!