disposable.js 771 B

1234567891011121314151617181920212223242526272829
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const isPromiseLike = (value) => typeof value === 'object' && !!value && 'then' in value;
  4. /**
  5. * A helper function that calls `.dispose()` on the {@link IDisposable} when
  6. * the given function (or promise returned by the function) returns.
  7. */
  8. exports.using = (disposable, fn) => {
  9. let ret;
  10. try {
  11. ret = fn(disposable);
  12. }
  13. catch (e) {
  14. disposable.dispose();
  15. throw e;
  16. }
  17. if (!isPromiseLike(ret)) {
  18. disposable.dispose();
  19. return ret;
  20. }
  21. return ret.then(value => {
  22. disposable.dispose();
  23. return value;
  24. }, err => {
  25. disposable.dispose();
  26. throw err;
  27. });
  28. };
  29. //# sourceMappingURL=disposable.js.map