728x90
반응형
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// promise.js | |
function countdown(seconds) { | |
return new Promise(function(resolve, reject) { | |
for(let i=seconds; i>=0; i--) { | |
setTimeout(function() { | |
if(i>0) console.log(i + '...'); | |
else resolve(console.log("GO!")); | |
}, (seconds-i)*1000); | |
} | |
}); | |
} | |
countdown(5).then( | |
function() { | |
console.log("countdown completed successfully"); | |
}, | |
function(err) { | |
console.log("countdown experienced an error: " + err.message); | |
} | |
); | |
/* | |
const p = countdown(5); | |
p.then(function() { | |
console.log("countdown completed successfully"); | |
}); | |
p.catch(function(err) { | |
console.log("countdown experienced an error: " + err.message); | |
}); | |
*/ |
728x90
반응형
'Javascript' 카테고리의 다른 글
terminal-image-cli : 터미널에 이미지 표시 (JS) (1) | 2024.10.25 |
---|---|
Javascript Promise 2 (0) | 2018.11.22 |
Callback Hell (0) | 2018.11.22 |
Error first callback (0) | 2018.11.22 |
String.raw (0) | 2018.11.22 |