Skip to content

Instantly share code, notes, and snippets.

@hyuki
Created July 4, 2020 20:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hyuki/323844ca57d8a123d99a6e1f95c15ff0 to your computer and use it in GitHub Desktop.
Save hyuki/323844ca57d8a123d99a6e1f95c15ff0 to your computer and use it in GitHub Desktop.
promise.html - A JavaScript Promise Example.
<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