大家好!
我使用 Node.js v8.5.0。 这是我在 Node.js 中的代码(简化版):
// function that returns promise
const request = (url) =>
new Promise((resolve, reject) => {
superagent
.get(url)
.end((err, res) => {
if (err || !res.ok) {
reject(err);
return;
}
resolve(res);
})
})
// function that add .then and .catch to promise
const to = promise =>
promise
.then((data) => [null, data])
.catch((err) => [err]);
// making request
let [err, result] = await to(request());
当我发出请求并发生一些错误时,.catch 函数无法捕获被拒绝的值,并且我收到诸如未处理的 promise 拒绝之类的错误。 但是,事实上我在promise中添加了.catch函数。
有人知道这里出了什么问题吗? 感谢帮助!
最佳答案
super 代理请求已返回 promise ,因此请使用它
const request = (url) => superagent.get(url).then(res=> {/*transform res**/ return res})
关于javascript - .catch 函数未捕获拒绝的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47776881/