Hi everyone! I'm the author of code.mu :)
I'd love to chat with my non-Russian audience. I'm looking for your feedback on the site and the translation quality. Let's chat:)
⊗jsSpPrmEC 165 of 294 menu
◀ ▶

Individual exception catching in promises in JavaScript

In then, you can specify only handler function of an exception by passing null instead of the first parameter:

promise.then( null, function(error) { console.log(error); } );

In this case, it is convenient to use a shortened syntax using the catch method:

promise.catch( function(error) { console.log(error); } );

Rewrite the following code via the catch method:

let promise = new Promise(function(resolve, reject) { setTimeout(function() { let isError = false; if (!isError) { resolve('success'); } else { reject(new Error('error')); } }, 3000); }); promise.then( res => console.log(res), err => console.log(err.message), );
← →
↑
English▼
AfrikaansAzərbaycanБългарскиবাংলাБеларускаяČeštinaDanskDeutschΕλληνικάEspañolEestiSuomiFrançaisहिन्दीMagyarՀայերենIndonesiaItaliano日本語ქართულიҚазақ한국어КыргызчаLietuviųLatviešuМакедонскиMelayuမြန်မာNederlandsNorskPolskiPortuguêsRomânăРусскийසිංහලSlovenčinaSlovenščinaShqipСрпскиSrpskiSvenskaKiswahiliТоҷикӣไทยTürkmenTürkçeЎзбекOʻzbekTiếng Việt
We use cookies for website operation, analytics, and personalization. Data processing is carried out in accordance with the Privacy Policy.
accept all customize decline