Created
July 4, 2020 20:43
-
-
Save hyuki/323844ca57d8a123d99a6e1f95c15ff0 to your computer and use it in GitHub Desktop.
promise.html - A JavaScript Promise Example.
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
<script> | |
/* cf. https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Promise */ | |
'use strict'; | |
var promiseCount = 0; | |
function runPromise() { | |
let id = promiseCount++; | |
console.log("runPromise: START: " + id); | |
new Promise( | |
(resolve, reject) => { | |
console.log("runPromise: EXEC: " + id); | |
window.setTimeout( | |
function() { | |
if (Math.random() > 0.5) { | |
resolve(id); | |
} else { | |
reject(id); | |
} | |
}, | |
Math.random() * 6000 | |
); | |
} | |
).then( | |
(arg) => { | |
console.log("RESOLVED: " + arg); | |
} | |
).catch( | |
(arg) => { | |
console.log("REJECTED: " + arg); | |
} | |
); | |
console.log("runPromise: END: " + id); | |
} | |
console.log("START"); | |
window.setInterval(runPromise, 3000); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment