Created
May 6, 2021 20:47
-
-
Save amsterdamharu/8aeb9b4990223546260ee0b756b9a901 to your computer and use it in GitHub Desktop.
Resolve last created promise
This file contains hidden or 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
const REPLACED = 'REPLACED'; | |
const last = (fn) => { | |
const current = { value: {} }; | |
return (...args) => { | |
const now = {}; | |
current.value = now; | |
return Promise.resolve(args) | |
.then((args) => fn(...args)) | |
.then((resolve) => | |
current.value === now | |
? resolve | |
: Promise.reject(REPLACED) | |
); | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment