You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
21 lines
344 B
21 lines
344 B
4 weeks ago
|
module.exports = thunkyp
|
||
|
|
||
|
function thunkyp (fn) {
|
||
|
let running = null
|
||
|
|
||
|
return ready
|
||
|
|
||
|
function ready () {
|
||
|
if (running) return running
|
||
|
const p = fn()
|
||
|
if (!(p instanceof Promise)) running = Promise.resolve(p)
|
||
|
else running = p
|
||
|
running.catch(onerror)
|
||
|
return running
|
||
|
}
|
||
|
|
||
|
function onerror () {
|
||
|
running = null
|
||
|
}
|
||
|
}
|