I am in the process of developing a wrapper for promise-returning functions that ensures each call waits for the previous ones to complete before executing.
Although I have a JavaScript implementation available, I am facing challenges in defining the types for the TypeScript implementation. How can I specify the fn
parameter as a generic function that returns a promise?
function serializePromises(fn) {
// This promise initializes our promise queue
let last = Promise.resolve();
return function (...args) {
// Catch is crucial here to prevent a rejection in a promise from disrupting the serialization process indefinitely
last = last.catch(() => {}).then(() => fn(...args));
return last;
}
}