fix: 修复success/complete的时序问题 (#78)
This commit is contained in:
14
README.md
14
README.md
@@ -483,3 +483,17 @@ weRequest.init({
|
||||
// ...
|
||||
})
|
||||
```
|
||||
|
||||
### 回调的success和complete的时序怎么和wx.request的时序不一致?
|
||||
|
||||
早期请求库引入promise能力时,由于代码缺陷导致success/complete执行的顺序与官方wx.request的success/complete执行顺序是相反的。(正确的顺序应该是先执行success回调,再执行complete回调)。
|
||||
由于这个问题存在比较久且很细微,可能大部分业务在使用的时候也没有太留意。
|
||||
请求库在`1.7.5`版本中对其进行了修复,但是为了不影响之前使用了本库的业务,所以增加了一个配置项`isFixSuccessCompleteTiming`,用于指定修复这个问题。
|
||||
若不配置这个项,则仍保持这个错误的回调顺序,若业务希望顺序跟官方保持一致,则需要在初始化时配置这个配置项,示例如下:
|
||||
```javascript
|
||||
weRequest.init({
|
||||
// ...
|
||||
isFixSuccessCompleteTiming: true
|
||||
// 省略其他配置项...
|
||||
})
|
||||
```
|
||||
1
build/interface.d.ts
vendored
1
build/interface.d.ts
vendored
@@ -28,6 +28,7 @@ export interface IInitOption {
|
||||
backupDomainList?: IAnyObject;
|
||||
backupDomainEnableCallback?: Function;
|
||||
domainChangeTrigger?: Function;
|
||||
isFixSuccessCompleteTiming: boolean;
|
||||
}
|
||||
export interface ICodeToSessionOptions {
|
||||
url: string;
|
||||
|
||||
File diff suppressed because one or more lines are too long
4
build/weRequest.min.js
vendored
4
build/weRequest.min.js
vendored
File diff suppressed because one or more lines are too long
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "we-request",
|
||||
"version": "1.7.4",
|
||||
"version": "1.7.5",
|
||||
"description": "本工具通过拓展小程序的wx.request,让开发者通过简单的配置,实现自动管理登录态等功能",
|
||||
"keywords": [
|
||||
"登录态",
|
||||
|
||||
@@ -62,6 +62,8 @@ export interface IInitOption {
|
||||
backupDomainEnableCallback?: Function;
|
||||
/* 是否需要启用备用域名 */
|
||||
domainChangeTrigger?: Function;
|
||||
/* 是否修复请求的success/complete的时序问题,详见README的QA部分 */
|
||||
isFixSuccessCompleteTiming: boolean;
|
||||
}
|
||||
|
||||
export interface ICodeToSessionOptions{
|
||||
|
||||
@@ -168,12 +168,23 @@ function doRequest(obj: IRequestOption) {
|
||||
return reject({ type: 'system-error', res });
|
||||
},
|
||||
complete() {
|
||||
if (config.isFixSuccessCompleteTiming) {
|
||||
setTimeout(()=>{
|
||||
if (typeof obj.complete === "function") {
|
||||
obj.complete();
|
||||
}
|
||||
if (obj.showLoading) {
|
||||
loading.hide();
|
||||
}
|
||||
}, 0)
|
||||
} else {
|
||||
if (typeof obj.complete === "function") {
|
||||
obj.complete();
|
||||
}
|
||||
if (obj.showLoading) {
|
||||
loading.hide();
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
@@ -201,12 +212,23 @@ function doUploadFile(obj: IUploadFileOption) {
|
||||
return reject({ type: 'system-error', res });
|
||||
},
|
||||
complete() {
|
||||
if (config.isFixSuccessCompleteTiming) {
|
||||
setTimeout(()=>{
|
||||
if (typeof obj.complete === "function") {
|
||||
obj.complete();
|
||||
}
|
||||
if (obj.showLoading) {
|
||||
loading.hide();
|
||||
}
|
||||
}, 0)
|
||||
} else {
|
||||
if (typeof obj.complete === "function") {
|
||||
obj.complete();
|
||||
}
|
||||
if (obj.showLoading) {
|
||||
loading.hide();
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
@@ -202,8 +202,6 @@ async function code2Session(code: string) {
|
||||
return reject({type: "http-error", res});
|
||||
}
|
||||
},
|
||||
complete() {
|
||||
},
|
||||
fail: (res) => {
|
||||
// 如果主域名不可用,且配置了备份域名,且本次请求未使用备份域名
|
||||
if ((config.domainChangeTrigger && config.domainChangeTrigger(res)) && url.isInBackupDomainList(obj.url)) {
|
||||
|
||||
@@ -40,7 +40,9 @@ const defaultConfig: IInitOption = {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
},
|
||||
// 是否修复请求的success/complete的时序问题,详见README的QA部分
|
||||
isFixSuccessCompleteTiming: false
|
||||
};
|
||||
|
||||
export default defaultConfig;
|
||||
|
||||
Reference in New Issue
Block a user