const httpRequest…

打捞于2024-02-19 23:24 阅读2.12K NO.000651
const httpRequest = async (options, method, onlyBody = true) => {
options = typeof options === 'string' ? { url: options } : options
typeof method === 'undefined' ? ('body' in options ? (method = 'post') : (method = 'get')) : (method = method)
const timeout = options?.timeout ? options.timeout : 15e3
const http = [
new Promise((_, e) => setTimeout(() => e('当前请求已超时'), timeout)),
new Promise((resolve, reject) => {
$.http[method.toLowerCase()](options)
.then((response) => {
let res = onlyBody ? response.body : response
res = $.toObj(res) || res
resolve(res)
})
.catch((err) => reject(err))
})
]
return await Promise.race(http)
.then((res) => res)
.catch((err) => err)
}

← 返回首页