简体   繁体   English

如何在javascript上导入和导出异步function?

[英]how to import and export asynchronous function on javascript?

i have this asynchronous function我有这个异步 function

//example get
  async getDatafromAPINODE(url) {
    try {
      let respond = await axios.get(API_URL_NODE + url, {
        headers: {
          "Content-Type": "application/json",
          Authorization: "Bearer " + this.state.tokenUser,
        },
      });
      if (respond.status >= 200 && respond.status < 300) {
        console.log("respond Post Data", respond);
      }
      return respond;
    } catch (err) {
      let respond = err;
      console.log("respond Post Data err", err);
      return respond;
    }
  }

how can i make this function export-able?我怎样才能使这个 function 能够出口? so i can use in another file with import所以我可以在另一个文件中使用import

ok this what im doing好的,这就是我在做什么

//asyncFunction.js
export const getDatafromAPINODE = async (url, props) => {
  try {
    let respond = await axios.get(API_URL_NODE + url, {
      headers: {
        "Content-Type": "application/json",
        Authorization: "Bearer " + props,
      },
    });
    if (respond.status >= 200 && respond.status < 300) {
      console.log("respond Post Data", respond);
    }
    return respond;
  } catch (err) {
    let respond = err;
    console.log("respond Post Data err", err);
    return respond;
  }
};

then i called using然后我打电话给使用

import {getDatafromAPINODE} from '../../../helper/asyncFunction'

as you can see im using 2 args url and props, the props will be used for getting the token for my need如您所见,我使用 2 个参数 url 和道具,道具将用于获取我需要的令牌

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM