修复resful请求返回值注入的Promise问题
parent
b91477cca9
commit
86136434ed
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "@bytedo/fetch",
|
||||
"version": "2.0.1",
|
||||
"version": "2.0.2",
|
||||
"description": "全新的ajax封装。分2个版本, 一个基于XMLHttpRequest, 一个基于window.fetch",
|
||||
"main": "dist/index.js",
|
||||
"files": [
|
||||
|
|
|
@ -282,16 +282,22 @@ class _Request {
|
|||
__success__(isSucc, result) {
|
||||
var { body, status, statusText, headers } = result
|
||||
var response = new Response(body, { status, statusText, headers })
|
||||
var _type
|
||||
|
||||
if (this._owner._inject_res) {
|
||||
response = this._owner._inject_res(response)
|
||||
_type = toS.call(response)
|
||||
}
|
||||
|
||||
if (isSucc) {
|
||||
this.defer.resolve(response)
|
||||
} else {
|
||||
if (_type === '[object Promise]') {
|
||||
return response.then(_ => this.defer.reject(_)).catch(_ => this.defer.reject(_))
|
||||
} else {
|
||||
this.defer.reject(response)
|
||||
}
|
||||
}
|
||||
delete this.xhr
|
||||
delete this.options
|
||||
delete this.defer
|
||||
|
|
|
@ -179,14 +179,20 @@ class _Request {
|
|||
.then(r => {
|
||||
clearTimeout(this.timer)
|
||||
let isSucc = r.status >= 200 && r.status < 400
|
||||
let _type
|
||||
if (this._owner._inject_res) {
|
||||
r = this._owner._inject_res(r)
|
||||
_type = toS.call(r)
|
||||
}
|
||||
if (isSucc) {
|
||||
return r
|
||||
} else {
|
||||
if (_type === '[object Promise]') {
|
||||
return r.then(_ => Promise.reject(_))
|
||||
} else {
|
||||
return Promise.reject(r)
|
||||
}
|
||||
}
|
||||
})
|
||||
.catch(e => {
|
||||
clearTimeout(this.timer)
|
||||
|
|
Loading…
Reference in New Issue