Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
function startCountingForHideAndSeek() {
// State for managing cleanup and cancelling
let finished = false;
let cancel = () => finished = true;
const promise = new Promise((resolve, reject) => {
//
// Custom Promise Logic
//
// NOTE: This countdown not finish in exactly 10 seconds, don't build timers this way!
// (this is as reliable as a child counting during hide-and-seek... :) )
// Good explanation can be found here: https://medium.com/javascript-in-plain-english/usetime-react-hook-f57979338de
let counts = 0;
const id = setInterval(() => {
counts += 1;
console.log(counts);
if (counts === 10) {
// Happy-path scenario
console.log('Ready or not, here I come!');
clearInterval(id);
resolve();
}
}, 1000);
// When consumer calls `cancel`:
cancel = () => {
// In case the promise has already resolved/rejected, don't run cancel behavior!
if (finished) {
return;
}
// Cancel-path scenario
console.log('OK, I\'ll stop counting.');
clearInterval(id);
reject();
};
// If was cancelled before promise was launched, trigger cancel logic
if (finished) {
// (to avoid duplication, just calling `cancel`)
cancel();
}
})
// After any scenario, set `finished = true` so cancelling has no effect
.then((resolvedValue) => {
finished = true;
return resolvedValue;
})
.catch((err) => {
finished = true;
return err;
});
// ES6 Promises do not have a built-in mechanism for cancelling promises.
// To support that feature, you need to provide the cancel callback
// and consider the possible timings:
// - cancelled before promise execution
// - cancelled during promise execution
// - cancelled after promise execution
// - cancel requested multiple times
return { promise, cancel };
}
/*
https://medium.com/javascript-in-plain-english/canceling-promises-in-javascript-31f4b8524dcd
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment