【问题标题】:axios couple of `.then` promises - how to combine them?axios 的几个 .then 承诺 - 如何组合它们?
【发布时间】:2016-12-25 12:24:02
【问题描述】:

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

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") })

如何向query() 函数添加另一个.then()

【问题讨论】:

    标签: javascript ajax axios


    【解决方案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
      })
    }
    

    【讨论】:

      猜你喜欢
      • 2021-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-17
      • 2015-01-23
      • 2017-09-04
      • 1970-01-01
      • 2016-01-21
      相关资源
      最近更新 更多