taro icon indicating copy to clipboard operation
taro copied to clipboard

Taro.request 返回的Promise 如何实现finally呢

Open sfuser001 opened this issue 8 years ago • 8 comments

sfuser001 avatar Oct 17 '18 02:10 sfuser001

哦哦 知道了

sfuser001 avatar Oct 17 '18 03:10 sfuser001

怎么实现的 @sfuser001

Eamonnzhang avatar Nov 21 '18 10:11 Eamonnzhang

@sfuser001 怎么解决?

qwIvan avatar Jan 14 '19 08:01 qwIvan

@sfuser001 怎么解决?

这样,随便写一个文件,例如:promise.js

内容如下:

Promise.prototype.finally = function (callback) { let P = this.constructor; return this.then( value => P.resolve(callback && callback()).then(() => value), reason => P.resolve(callback && callback()).then(() => { throw reason }) ); }; 在app.js中引入 import 'promise.js';

然后,如果你要是使用的ts的话,请在 tsconfig.json中 把compilerOptions中的target 修改成 es2018

chenkefeng avatar Jan 14 '19 08:01 chenkefeng

额 这个就是我 我另外一个账号忘记了😓

chenkefeng avatar Jan 14 '19 08:01 chenkefeng

@chenkefeng 没有用ts,不知道能不能通过babel配置来实现。

qwIvan avatar Jan 14 '19 09:01 qwIvan

@chenkefeng 没有用ts,不知道能不能通过babel配置来实现。

不用ts上边的方法也行。直接引入 promise.js就好了 内容如下: Promise.prototype.finally = function (callback) { let P = this.constructor; return this.then( value => P.resolve(callback && callback()).then(() => value), reason => P.resolve(callback && callback()).then(() => { throw reason }) ); };

chenkefeng avatar Jan 14 '19 10:01 chenkefeng

@chenkefeng 没有用ts,不知道能不能通过babel配置来实现。

不用ts上边的方法也行。直接引入 promise.js就好了 内容如下: Promise.prototype.finally = function (callback) { let P = this.constructor; return this.then( value => P.resolve(callback && callback()).then(() => value), reason => P.resolve(callback && callback()).then(() => { throw reason }) ); };

我还是觉得babel能解决这个问题

qwIvan avatar Jan 15 '19 10:01 qwIvan