NewFuture / miniprogram-network

Redefine the Network API of Wechat MiniProgram (小程序网络库)
https://miniprogram-network.newfuture.cc
Apache License 2.0
113 stars 11 forks source link

对于transformResponse能不能提供一下reject或者throw error的例子? #26

Closed jechtliu closed 5 years ago

NewFuture commented 5 years ago

同步操作直接 throw

比如内置的 transformRequestResponseOkData

{
   transformResponse: (res)=>{
        if (res.statusCode < 200 || res.statusCode >= 300) { throw res;}
        return res.data
   }
}
NewFuture commented 5 years ago

也可以是异步(promise )的, 需要 返回 reject即可

{
 transformResponse: (res, option) => {
  // 异步函数或者 new promise 都行
        return new Promise((resolve, reject) => {
            wx.getStorage({
                key: res.data as string,
                success: resolve,
                fail: reject,
            });
        });
    }
}