Last active
May 30, 2024 01:09
-
-
Save AshKyd/7e10c3e9d0b470b76a29a84df14fd819 to your computer and use it in GitHub Desktop.
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
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