Skip to content

Instantly share code, notes, and snippets.

@amsterdamharu
Created May 6, 2021 20:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amsterdamharu/8aeb9b4990223546260ee0b756b9a901 to your computer and use it in GitHub Desktop.
Save amsterdamharu/8aeb9b4990223546260ee0b756b9a901 to your computer and use it in GitHub Desktop.
Resolve last created promise
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