Skip to content

Instantly share code, notes, and snippets.

@jamesfulford
Created April 13, 2020 06:01
Show Gist options
  • Save jamesfulford/5958f8670d4ab3e70af09109cb64ee62 to your computer and use it in GitHub Desktop.
Save jamesfulford/5958f8670d4ab3e70af09109cb64ee62 to your computer and use it in GitHub Desktop.
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 };
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment