从函数中返回Axios Promise [英] Returning an Axios Promise from function

查看:198
本文介绍了从函数中返回Axios Promise的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以解释为什么返回Axios承诺允许进一步链接,但在应用之后返回then()/ catch()方法不会?

Can someone please explain why returning an Axios promise allows for further chaining, but returning after applying a then()/catch() method does not?

示例:

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/axios@0.16.1/dist/axios.min.js"></script>

< a href =https://jsfiddle.net/nandastone/81zdvodv/1/ =noreferrer> https://jsfiddle.net/nandastone/81zdvodv/1/

我知道Promise方法应该返回一个要链接的值,但为什么这两种返回方法之间存在差异?

I understand that Promise methods should return a value to be chained, but why is there a difference between these two return methods?

推荐答案

您的第一个示例返回原始承诺。您的第二个示例返回不同的承诺,通过调用 catch 创建的承诺。

Your first example returns the original promise. Your second example returns a different promise, the one created by calling catch.

两者之间的关键差异是:

The critical differences between the two are:


  1. 在你的第二个例子中,你没有传递分辨率值,所以你的返回的承诺然后 undefined 解决(的返回值> console.log )。

在第二个示例中,您将拒绝转换为 undefined (通过从 catch 中返回 console.log 的结果)。一个 catch 处理程序,它不会抛出或返回被拒绝的承诺,将拒绝转换为解决方案。

In your second example, you're converting rejections into resolutions with undefined (by returning the result of console.log out of catch). A catch handler that doesn't throw or return a promise that's rejected converts a rejection into a resolution.

关于承诺链的关键之一是他们改变了结果;每次调用然后 catch 创建一个新的承诺,他们的处理程序可以修改结果通过后发送到下游的内容它们。

One of the key things about promise chains is that they transform the result; every call to then or catch creates a new promise, and their handlers can modify what's sent downstream as the result passes through them.

通常的模式确实是返回链的结果,但链中的函数要么故意转换结果,要么传递它。通常情况下,你不会有一个 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 catch handler 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).

如果您只想记录通过的内容,同时仍然允许调用者看到它,但确实想要返回结果无论出于何种原因,您都可以这样做:

If you wanted to just log what passed through while still allowing callers to see it but did want 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); });

或使用 throw

return request
    .then(result => { console.log(result); return result; })
    .catch(error => { console.error(error); throw error; });

这篇关于从函数中返回Axios Promise的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆