ජාවාස්ක්‍රිප්ට් හි ප්‍රොමිස් වල වෙනම ව්‍යතිරේක අල්ලා ගැනීම

then හිදී, පළමු පරාමිතිය වෙනුවට null ලබා දෙමින් ව්‍යතිරේක තත්වය සඳහා වන ශ්‍රිතය පමණක් සඳහන් කළ හැක:

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

එවැනි අවස්ථාවන්හිදී, catch ක්‍රමය හරහා කෙටි වාක්‍ය ඛණ්ඩය භාවිතා කිරීම පහසුය:

javascriptpromise.catch(
  function(error) {
    console.log(error);
  }
);
Задание 1

පහත දක්වන කේතය catch ක්‍රමය හරහා නැවත ලියන්න:

javascriptlet 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),
);