Skip to content

Instantly share code, notes, and snippets.

@AshKyd
Last active May 30, 2024 01:09
Show Gist options
  • Save AshKyd/7e10c3e9d0b470b76a29a84df14fd819 to your computer and use it in GitHub Desktop.
Save AshKyd/7e10c3e9d0b470b76a29a84df14fd819 to your computer and use it in GitHub Desktop.
function cancellable() {
let isRunning = true;
const p = new Promise((resolve, reject) => {
setTimeout(() => {
if (!isRunning) return reject();
resolve();
}, 1000)
});
p.stoppp = () => (isRunning = false);
return p;
}
var a = cancellable();
a.stoppp();
a.then(() => console.log('a resolved')).catch(e => console.log('a caught', e))
var b = cancellable();
b.then(() => console.log('b resolved')).catch(e => console.log('b caught', e))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment