aswi2020vldc-gitlab/templetes/node_modules/thunky/promise.js @ 9b6ff40e
1 |
module.exports = thunkyp |
---|---|
2 |
|
3 |
function thunkyp (fn) { |
4 |
let running = null |
5 |
|
6 |
return ready |
7 |
|
8 |
function ready () { |
9 |
if (running) return running |
10 |
const p = fn() |
11 |
if (!(p instanceof Promise)) running = Promise.resolve(p) |
12 |
else running = p |
13 |
running.catch(onerror) |
14 |
return running |
15 |
}
|
16 |
|
17 |
function onerror () { |
18 |
running = null |
19 |
}
|
20 |
}
|