zoukankan      html  css  js  c++  java
  • Promise 解决同步请求问题

      在写小程序和vue项目中,由于 api 不提供 同步请求,因此,可以通过  Promise 来实现 同步请求操作

     在这里 对于 Promise 不太了解的小伙伴 可以查找 Promise 的api 文档

      下面是主要代码

    const axios = require('axios')
    function axiosPro(axiosArgs) {
      const { method, url, data, params, headers, responseType } = axiosArgs
      return new Promise(function (resolve, reject) {
        axios({
          method: method,
          url: url,
          data: data,
          params: params,
          headers: headers,
          responseType: responseType
        }).then(function (ret) {
          resolve(ret);
        }).catch(function (err) {
          reject(err);
        })
      })
    };
    
    引用
    module.exports = async ctx => {
      const ret = await axiosPro(ctx.request.body)
      console.log(ret.data)//同步输出结果
      ctx.body = ret.data
    }

        

      

  • 相关阅读:
    (QR14)带权的DAG节点排序
    数字组合
    最长连续不重复子序列
    树状数组
    归并排序
    差分
    前缀和
    64位整数乘法
    MySQL8 常用指令
    离线及实时实操架构
  • 原文地址:https://www.cnblogs.com/lvxisha/p/12035709.html
Copyright © 2011-2022 走看看