promise.js 344 B

1234567891011121314151617181920
  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. }