⊗jsSpPrmEO 164 of 294 menu

Object with a promise error in JavaScript

It is more common to pass to the reject function not a string with an error, but an object with an error:

let promise = new Promise(function(resolve, reject) { setTimeout(function() { reject(new Error('error in promise')); // an object with an error }, 3000); });

You can also throw objects with errors using throw - this will be equivalent to passing them to reject:

let promise = new Promise(function(resolve, reject) { setTimeout(function() { throw new Error('error in promise'); // the equivalent of reject }, 3000); });

Modify the following code according to what you learned:

let promise = new Promise(function(resolve, reject) { setTimeout(function() { let isError = false; if (!isError) { resolve('success'); } else { reject('error'); } }, 3000); });
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