Skip to content

Instantly share code, notes, and snippets.

@aquapi
Last active January 9, 2026 02:30
Show Gist options
  • Select an option

  • Save aquapi/1ab4ab25264729faaa02e8be26c801f4 to your computer and use it in GitHub Desktop.

Select an option

Save aquapi/1ab4ab25264729faaa02e8be26c801f4 to your computer and use it in GitHub Desktop.
const getFinishedState = async (s: [string], p: Promise<any>) => {
try {
await p;
s[0] = 'resolved';
} catch (e) {
s[0] = 'rejected';
// Don't swallow error
return p;
}
};
// p-state replacement
export const state = async (p: Promise<any>): Promise<'rejected' | 'resolved' | 'pending'> => {
const res = ['pending'] as [string];
getFinishedState(res, p);
await nextTick;
return res[0] as any;
};
// p-waterfall replacement, change for loop to for of for other types of iterable
export const waterfall = (tasks: (value: any) => any, initialValue?: any): any => {
let res = Promise.resolve(initialValue);
for (let i = 0; i < tasks.length; i++) res = res.then(tasks[i]);
return res;
}
// p-is-promise replacement
export const isThenable = <T>(p: any): p is PromiseLike<T> =>
p !== null &&
typeof p === 'object' &&
!Array.isArray(p) &&
Object.hasOwn(p, 'then');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment