disposable.js 699 B

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