axios夫妇`.then`承诺 - 如何结合它们?

时间:2016-08-18 14:10:17

标签: javascript ajax axios

假设我有一个axios函数的包装器 - 应该在每个ajax查询上实现,所以我想保留代码DRY。像那样:

import axios     from "axios"
import NProgress from "nprogress"

const query = (url, options) => {
  NProgress.start()

  return axios({
    url: url,
    method: options.method || "GET",
    data: options.data || {}
  }).then(() => {
    NProgress.done()
  })
}

export default query

问题在于,如果我将.then解析器添加到query(),则不会发生任何事情!像那样:

从" ./ query.js"

导入查询
query("something", {}).then(() => { console.log("This will never logged") })

如何向.then()功能添加其他query()

1 个答案:

答案 0 :(得分:1)

回复一些东西!

const query = (url, options) => {
  NProgress.start()

  return axios({
    url: url,
    method: options.method || "GET",
    data: options.data || {}
  }).then((response) => {
    NProgress.done()
    return response // change is here
  })
}