unifyPromiseVue2.ts 808 B

12345678910111213141516171819202122232425262728
  1. export default function unifyPromiseVue2() {
  2. try {
  3. // eslint-disable-next-line no-inner-declarations
  4. function isPromise(obj) {
  5. return Boolean(obj) && (typeof obj === 'object' || typeof obj === 'function') && typeof obj.then === 'function';
  6. }
  7. // Unified vue2 API Promise return format to be consistent with vue3
  8. // eslint-disable-next-line no-undef
  9. (uni as any).addInterceptor({
  10. returnValue(res) {
  11. if (!isPromise(res)) {
  12. return res;
  13. }
  14. return new Promise((resolve, reject) => {
  15. res.then((res) => {
  16. if (res[0]) {
  17. reject(res[0]);
  18. } else {
  19. resolve(res[1]);
  20. }
  21. });
  22. });
  23. },
  24. });
  25. // eslint-disable-next-line no-empty
  26. } catch (error) { }
  27. }