FrontEnd.ro logo
Promise-uri în acțiune
Promise.resolve(Date.now())
  .then((miliseconds) => {
    if (miliseconds % 2 === 0) {
      return miliseconds / 1000;
    }
    throw new Error('Miliseconds are an odd number');
  })
  .catch((reason) => {
    console.log('Defaulting to -1');
    return -1;
  })
  .then((seconds) => {
    if (seconds % 2 === 0) {
      return seconds / 60;
    }
    throw new Error('Seconds are an odd number');
  })
  .then((minutes) => {
    console.log(`Success: minutes=${minutes}`);
  })
  .catch((reason) => {
    console.log(reason);
  })
  .finally(() => {
    console.log("This will be executed at the end!");
  });