Created
May 30, 2023 11:10
-
-
Save mmyoji/c0a749fd00923fad84bd9f2fb13aec53 to your computer and use it in GitHub Desktop.
Make a function as async one
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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