Javascript 从函数返回一个 Axios Promise
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43463989/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Returning an Axios Promise from function
提问by Matt Stone
Can someone please explain why returning an Axios promise allows for further chaining, but returning after applying a then()/catch()method does not?
有人可以解释为什么返回 Axios 承诺允许进一步链接,但在应用then()/catch()方法后返回不允许吗?
Example:
例子:
const url = 'https://58f58f38c9deb71200ceece2.mockapi.io/Mapss'
function createRequest1() {
const request = axios.get(url)
request
.then(result => console.log('(1) Inside result:', result))
.catch(error => console.error('(1) Inside error:', error))
return request
}
function createRequest2() {
const request = axios.get(url)
return request
.then(result => console.log('(2) Inside result:', result))
.catch(error => console.error('(2) Inside error:', error))
}
createRequest1()
.then(result => console.log('(1) Outside result:', result))
.catch(error => console.error('(1) Outside error:', error))
createRequest2()
.then(result => console.log('(2) Outside result:', result))
.catch(error => console.error('(2) Outside error:', error))
<script src="https://unpkg.com/[email protected]/dist/axios.min.js"></script>
https://jsfiddle.net/nandastone/81zdvodv/1/
https://jsfiddle.net/nandastone/81zdvodv/1/
I understand that Promise methods should return a value to be chained, but why is there a difference between these two return methods?
我知道 Promise 方法应该返回一个要链接的值,但是为什么这两个返回方法之间存在差异?
回答by T.J. Crowder
Your first example returns the original promise. Your second example returns a differentpromise, the one created by calling catch.
您的第一个示例返回原始承诺。您的第二个示例返回一个不同的承诺,即通过调用catch.
The critical differences between the two are:
两者之间的关键区别是:
In your second example, you're not passing on the resolution value, so the promise returned by your
thenis resolved withundefined(the return value ofconsole.log).In your second example, you're converting rejections into resolutions with
undefined(by returning the result ofconsole.logout ofcatch). Acatchhandler that doesn't throw or return a promise that's rejected converts a rejection into a resolution.
在你的第二个例子中,你没有传递分辨率值,所以你返回的承诺
then是用undefined(的返回值console.log)解决的。在您的第二个示例中,您将拒绝转换为解决方案
undefined(通过返回console.logout of的结果catch)。catch不抛出或返回被拒绝的承诺的处理程序将拒绝转换为解决方案。
One of the key things about promise chains is that they transform the result; every call to thenor catchcreates a new promise, and their handlers can modify what's sent downstream as the result passes through them.
Promise 链的关键之一是它们可以转换结果;每次调用then或catch创建一个新的承诺,当结果通过它们时,它们的处理程序可以修改向下游发送的内容。
The usual pattern would indeed be to return the result of the chain, but for the functions in the chain to either intentionally transform the result or pass it on. Normally, you wouldn't have a catchhandler except at the terminal end of the chain, unless you're using it to correct the error condition (intentionally converting a rejection into a resolution).
通常的模式确实是返回链的结果,但链中的函数要么有意地转换结果,要么传递它。通常,catch除了在链的终端之外,您不会有处理程序,除非您使用它来纠正错误情况(有意将拒绝转换为解决方案)。
If you wanted to just log what passed through while still allowing callers to see it but didwant to return the result of the chain for whatever reason, you'd do this:
如果您只想记录通过的内容,同时仍然允许调用者看到它,但出于某种原因确实想返回链的结果,您可以这样做:
return request
.then(result => { console.log(result); return result; })
.catch(error => { console.error(error); return Promise.reject(error); });
or using throw:
或使用throw:
return request
.then(result => { console.log(result); return result; })
.catch(error => { console.error(error); throw error; });

