From 86136434ed50c5aeb90face0214eeccf8689c77f Mon Sep 17 00:00:00 2001 From: yutent Date: Fri, 10 Jun 2022 14:00:24 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dresful=E8=AF=B7=E6=B1=82?= =?UTF-8?q?=E8=BF=94=E5=9B=9E=E5=80=BC=E6=B3=A8=E5=85=A5=E7=9A=84Promise?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 2 +- src/index.js | 8 +++++++- src/next.js | 8 +++++++- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 3bde410..a373491 100644 --- a/package.json +++ b/package.json @@ -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": [ diff --git a/src/index.js b/src/index.js index 2463bfa..ea6925e 100644 --- a/src/index.js +++ b/src/index.js @@ -282,15 +282,21 @@ 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 { - 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.options diff --git a/src/next.js b/src/next.js index 24a5d61..01f851b 100644 --- a/src/next.js +++ b/src/next.js @@ -179,13 +179,19 @@ 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 { - return Promise.reject(r) + if (_type === '[object Promise]') { + return r.then(_ => Promise.reject(_)) + } else { + return Promise.reject(r) + } } }) .catch(e => {