Created
July 30, 2018 09:57
-
-
Save kebot/ff97e8f63c7f99cf33cdcf75d41efe38 to your computer and use it in GitHub Desktop.
hfn - hot functions
This file contains hidden or 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
const asyncOnce = (fn) => { | |
const promise | |
return (...args) => { | |
if (!promise) { | |
promise = fn.apply(null, args) | |
} | |
return promise | |
} | |
} | |
const init = asyncOnce(() => { | |
console.log('init') | |
return new Promise((resolve, reject) => { | |
global.setTimeout(() => { | |
resolve(true) | |
}, 1000) | |
}) | |
}) | |
;(async () => { | |
await Promise.all([init(), init()]) | |
console.log(await init()) | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment