修复resful请求返回值注入的Promise问题

master
yutent 2022-06-10 14:00:24 +08:00
parent b91477cca9
commit 86136434ed
3 changed files with 15 additions and 3 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "@bytedo/fetch", "name": "@bytedo/fetch",
"version": "2.0.1", "version": "2.0.2",
"description": "全新的ajax封装。分2个版本, 一个基于XMLHttpRequest, 一个基于window.fetch", "description": "全新的ajax封装。分2个版本, 一个基于XMLHttpRequest, 一个基于window.fetch",
"main": "dist/index.js", "main": "dist/index.js",
"files": [ "files": [

View File

@ -282,15 +282,21 @@ class _Request {
__success__(isSucc, result) { __success__(isSucc, result) {
var { body, status, statusText, headers } = result var { body, status, statusText, headers } = result
var response = new Response(body, { status, statusText, headers }) var response = new Response(body, { status, statusText, headers })
var _type
if (this._owner._inject_res) { if (this._owner._inject_res) {
response = this._owner._inject_res(response) response = this._owner._inject_res(response)
_type = toS.call(response)
} }
if (isSucc) { if (isSucc) {
this.defer.resolve(response) this.defer.resolve(response)
} else { } else {
this.defer.reject(response) if (_type === '[object Promise]') {
return response.then(_ => this.defer.reject(_)).catch(_ => this.defer.reject(_))
} else {
this.defer.reject(response)
}
} }
delete this.xhr delete this.xhr
delete this.options delete this.options

View File

@ -179,13 +179,19 @@ class _Request {
.then(r => { .then(r => {
clearTimeout(this.timer) clearTimeout(this.timer)
let isSucc = r.status >= 200 && r.status < 400 let isSucc = r.status >= 200 && r.status < 400
let _type
if (this._owner._inject_res) { if (this._owner._inject_res) {
r = this._owner._inject_res(r) r = this._owner._inject_res(r)
_type = toS.call(r)
} }
if (isSucc) { if (isSucc) {
return r return r
} else { } else {
return Promise.reject(r) if (_type === '[object Promise]') {
return r.then(_ => Promise.reject(_))
} else {
return Promise.reject(r)
}
} }
}) })
.catch(e => { .catch(e => {