Fullstack React: Introduction to Promises

PHOTO EMBED

Mon May 09 2022 19:27:38 GMT+0000 (Coordinated Universal Time)

Saved by @joel113 #javascript

function getCurrentTime() {
  // Get the current 'global' time from an API using Promise
  return new Promise((resolve, reject) => {
    setTimeout(function() {
      var didSucceed = Math.random() >= 0.5;
      didSucceed ? resolve(new Date()) : reject('Error');
    }, 2000);
  })
}
getCurrentTime()
  .then(currentTime => getCurrentTime())
  .then(currentTime => {
    console.log('The current time is: ' + currentTime);
    return true;
  })
  .catch(err => console.log('There was an error:' + err))
content_copyCOPY

https://www.newline.co/fullstack-react/30-days-of-react/day-15/