Skip to content

Instantly share code, notes, and snippets.

@mmyoji
Created May 30, 2023 11:10
Show Gist options
  • Save mmyoji/c0a749fd00923fad84bd9f2fb13aec53 to your computer and use it in GitHub Desktop.
Save mmyoji/c0a749fd00923fad84bd9f2fb13aec53 to your computer and use it in GitHub Desktop.
Make a function as async one
// see: https://www.bbss.dev/posts/eventloop/
/**
* @example
* ```ts
* const fn = makePromise(() => {
* return heavyFunc();
* });
* // or you can write:
* // const fn = makePromise(heavyFunc);
* const result = await fn();
* ```
*/
export function makePromise<T>(fn: () => T): Promise<T> {
return new Promise((resolve) => {
setTimeout(() => {
resolve(fn());
}, 0);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment