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.

20 lines
344 B

4 years ago
  1. module.exports = thunkyp
  2. function thunkyp (fn) {
  3. let running = null
  4. return ready
  5. function ready () {
  6. if (running) return running
  7. const p = fn()
  8. if (!(p instanceof Promise)) running = Promise.resolve(p)
  9. else running = p
  10. running.catch(onerror)
  11. return running
  12. }
  13. function onerror () {
  14. running = null
  15. }
  16. }