小程序发送请求的异步操作探索

回调

一开始的时候使用回调的方式写统一调用小程序发送请求的接口。

准备数据 => 发送请求 => 执行回调

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
function CGI(yurl, type, data, success, err) {
var version = getVersion();
var uoAuth = getUoAuth();
var uoSign = getUoSign();
var url = getTrueUrl(yurl);
if (uoAuth && uoSign) {
data.uoAuth = uoAuth;
data.uoSign = uoSign;
}
//上面获取登陆状态,和参数的东西,无视
type = type.toUpperCase();
var contentType = 'application/json';
if (type == "POST") {
contentType = 'application/x-www-form-urlencoded';
}
wx.request({
url: url,
data: data,
method: type,
header: {
'content-type': contentType
},
success: function (res) {
if(res.data.errCode == 1) {
login();
return ;
}
typeof success == "function" && success(res.data);
},
fail: function (res) {
wx.showToast({
title: res.msg || '网络访问失败',
duration: 1500
});
typeof err == "function" && err(res.data);
}
})
}

Promise

将回调改写成 Promise 的方式调用

准备数据 => 发送请求 => 返回 Promise 对象 => then

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*
Promise 接口调用函数
*/
function PCGI(yurl, type, data, success, err) {
console.log('PCGI')
var version = getVersion();
var uoAuth = getUoAuth();
var uoSign = getUoSign();
var url = getTrueUrl(yurl);
if (uoAuth && uoSign) {
data.uoAuth = uoAuth;
data.uoSign = uoSign;
}

type = type.toUpperCase();
var contentType = 'application/json';
if (type == "POST") {
contentType = 'application/x-www-form-urlencoded';
}
return new Promise((resolve,reject)=>{
wx.request({
url: url,
data: data,
method: type,
header: {
'content-type': contentType
},
success: function (res) {
if (res.data.errCode == 1) {
login();
return;
}
resolve(res.data)
},
fail: function (res) {
wx.showToast({
title: res.msg || '网络访问失败',
duration: 1500
});
reject(res.data);
}
})
})
}

generator + co

在 PCGI 的基础上改造,支持并发请求

准备数据 => 发送请求 => 返回 Promise 对象 => then

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
const HEADER = {
'GET':{
'content-type': 'application/json'
},
'POST': {
'content-type':'application/x-www-form-urlencoded'
}
}
function getTrueUrl(url){
//...
}

function promiseReq(item){
let method = 'GET';
var uoAuth = getUoAuth() || '';
var uoSign = getUoSign() || '';
let config = {
url: '',
data: {uoAuth, uoSign},
method,
}
return new Promise((resolve,reject)=>{
if (isString(item)){
config.url = getTrueUrl(item);
}else{
let { url, type='GET', data } = item;
config.url = getTrueUrl(url);
config.data = data ? Object.assign(config.data, data) : config.data;
config.method = type && type.toUpperCase() || method;
}
config.header = HEADER[config.method];

config.success = (res) => resolve(res.data)
config.fail = (err) => reject(err)

wx.request(config);
})
}


function requestFn(request) {
console.log('requestFn')
function* task() {
try {
var result;
if(isArray(request)){
result = yield Promise.all(request.map((item, index, arr) => promiseReq(item)))
}else{
result = yield promiseReq(request)
}
console.log(result)
return result;
} catch (e) {
console.log(e)
}
}
return co(task)
}

async / await

在 generator + co 的基础上使用 async / await 改造

准备数据 => 发送请求 => 返回 Promise 对象 => then

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
function promiseReq(item){
let method = 'GET';
var uoAuth = getUoAuth() || '';
var uoSign = getUoSign() || '';
let config = {
url: '',
data: {uoAuth, uoSign},
method,
}
return new Promise((resolve,reject)=>{
if (isString(item)){
config.url = getTrueUrl(item);
}else{
let { url, type='GET', data } = item;
config.url = getTrueUrl(url);
config.data = data ? Object.assign(config.data, data) : config.data;
config.method = type && type.toUpperCase() || method;
}
config.header = HEADER[config.method];

config.success = (res) => resolve(res.data)
config.fail = (err) => reject(err)

wx.request(config);
})
}

function asyncRequest(request) {
console.log('asyncRequest')
return (async () => {
try {
return isArray(request) ? await Promise.all(request.map((item, index, arr) => promiseReq(item))) : await promiseReq(request);
} catch (e) {
console.log(e)
}
})()
}