Skip to content

Instantly share code, notes, and snippets.

@kebot
Created July 30, 2018 09:57
Show Gist options
  • Save kebot/ff97e8f63c7f99cf33cdcf75d41efe38 to your computer and use it in GitHub Desktop.
Save kebot/ff97e8f63c7f99cf33cdcf75d41efe38 to your computer and use it in GitHub Desktop.
hfn - hot functions
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