Guest User

Untitled

a guest
Feb 25th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. // Learning JavaScript
  2. // chapter 14 : Asynchronous Programming
  3.  
  4. // use Promise and Causing Errors
  5.  
  6. function countdown(seconds) {
  7. return new Promise(function(resolve, reject) {
  8. for (let i = seconds; i >= 0; i--) {
  9. setTimeout(function() {
  10. if (i === 13) return reject(new Error('Oh my GOD'));
  11. if (i > 0) console.log(i + '...');
  12. else resolve(console.log('GO!'));
  13. }, (seconds - i) * 1000);
  14. }
  15. });
  16. }
  17.  
  18. const countdownNum = 15;
  19. countdown(countdownNum).then(
  20. function() {
  21. console.log('countdown completed successfully');
  22. },
  23. function(err) {
  24. console.log('countdown experienced an error ' + err.message);
  25. }
  26. );
  27.  
  28. const p = countdown(countdownNum);
  29. p.then(function() {
  30. console.log('countdown completed successfully');
  31. });
  32. p.catch(function(err) {
  33. console.log('countdown experienced ann error: ' + err.message);
  34. });
Add Comment
Please, Sign In to add comment