web 3d图形渲染器
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.

30 lines
811 B

  1. 'use strict'
  2. module.exports.disposer = disposer
  3. function disposer (creatorFn, disposerFn, fn) {
  4. const runDisposer = (resource, result, shouldThrow = false) => {
  5. return disposerFn(resource)
  6. .then(
  7. // disposer resolved, do something with original fn's promise
  8. () => {
  9. if (shouldThrow) {
  10. throw result
  11. }
  12. return result
  13. },
  14. // Disposer fn failed, crash process
  15. (err) => {
  16. throw err
  17. // Or process.exit?
  18. })
  19. }
  20. return creatorFn
  21. .then((resource) => {
  22. // fn(resource) can throw, so wrap in a promise here
  23. return Promise.resolve().then(() => fn(resource))
  24. .then((result) => runDisposer(resource, result))
  25. .catch((err) => runDisposer(resource, err, true))
  26. })
  27. }