Skip to content

Instantly share code, notes, and snippets.

@fiuzagr
Created June 29, 2023 14:25
Show Gist options
  • Save fiuzagr/6b71081f24ac74d4a07aee61e727d1cd to your computer and use it in GitHub Desktop.
Save fiuzagr/6b71081f24ac74d4a07aee61e727d1cd to your computer and use it in GitHub Desktop.
Queue Promises
let _queue: Promise<any> = Promise.resolve(true);
const queuePromise = (operation: (value: any) => Promise<any>) => {
return new Promise((resolve, reject) => {
_queue = _queue
.then(operation)
.then(resolve)
.catch(reject);
});
};
export default queuePromise;
//
// usage:
// queuePromise(() => promise1.then()).then(finishExecution)
// queuePromise((result1) => promise2.then()).then(finishExecution)
// queuePromise((result2) => promiseN.then()).then(finishExecution)
//
// finishExecution will be run one time. Can be declared in the any queue().then
//
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment