4
0
mirror of https://gitee.com/dcloud/uni-preset-vue synced 2025-04-09 23:33:10 +08:00

chore: uni.promisify.adaptor.js

This commit is contained in:
DCloud_LXH 2023-04-07 15:33:24 +08:00
parent 4517f04736
commit c8f557ec23
4 changed files with 22 additions and 50 deletions

@ -1,32 +1,8 @@
import Vue from 'vue'
import App from './App.vue'
import './uni.promisify.adaptor'
Vue.config.productionTip = false
function isPromise(obj: any) {
return (
!!obj &&
(typeof obj === "object" || typeof obj === "function") &&
typeof obj.then === "function"
);
}
uni.addInterceptor({
returnValue(res: any) {
if (!isPromise(res)) {
return res;
}
return new Promise((resolve, reject) => {
res.then((res: [any, any]) => {
if (res[0]) {
reject(res[0]);
} else {
resolve(res[1]);
}
});
});
},
});
const app = new (typeof App === 'function' ? App : Vue.extend(Object.assign({ mpType: 'app' }, App)))
app.$mount();

@ -0,0 +1,10 @@
uni.addInterceptor({
returnValue (res) {
if (!(!!res && (typeof res === "object" || typeof res === "function") && typeof res.then === "function")) {
return res;
}
return new Promise((resolve, reject) => {
res.then((res: [unknown, unknown]) => res[0] ? reject(res[0]) : resolve(res[1]));
});
},
});

@ -1,35 +1,11 @@
import Vue from 'vue'
import App from './App'
import './uni.promisify.adaptor'
Vue.config.productionTip = false
App.mpType = 'app'
function isPromise(obj) {
return (
!!obj &&
(typeof obj === "object" || typeof obj === "function") &&
typeof obj.then === "function"
);
}
uni.addInterceptor({
returnValue(res) {
if (!isPromise(res)) {
return res;
}
return new Promise((resolve, reject) => {
res.then((res) => {
if (res[0]) {
reject(res[0]);
} else {
resolve(res[1]);
}
});
});
},
});
const app = new Vue({
...App
})

@ -0,0 +1,10 @@
uni.addInterceptor({
returnValue (res) {
if (!(!!res && (typeof res === "object" || typeof res === "function") && typeof res.then === "function")) {
return res;
}
return new Promise((resolve, reject) => {
res.then((res) => res[0] ? reject(res[0]) : resolve(res[1]));
});
},
});