Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Learning JavaScript
- // chapter 14 : Asynchronous Programming
- // use Promise and Causing Errors
- function countdown(seconds) {
- return new Promise(function(resolve, reject) {
- for (let i = seconds; i >= 0; i--) {
- setTimeout(function() {
- if (i === 13) return reject(new Error('Oh my GOD'));
- if (i > 0) console.log(i + '...');
- else resolve(console.log('GO!'));
- }, (seconds - i) * 1000);
- }
- });
- }
- const countdownNum = 15;
- countdown(countdownNum).then(
- function() {
- console.log('countdown completed successfully');
- },
- function(err) {
- console.log('countdown experienced an error ' + err.message);
- }
- );
- const p = countdown(countdownNum);
- p.then(function() {
- console.log('countdown completed successfully');
- });
- p.catch(function(err) {
- console.log('countdown experienced ann error: ' + err.message);
- });
Add Comment
Please, Sign In to add comment