Search code examples
node.jsvue.jsasynchronousexportdefault

Export default async function haven't execute the whole function during import in another js


I would like to define the axios baseURL by trigger internal API call in http-common.js, but when I import the module from http-common.js it can not obtain axios object. I found that even the async function haven't run ... Is anyone have idea on that ? Thanks a lot.

In http-common.js file

export default async function getAPIEndPoint() {
  var endpoint = await axios.get("http://localhost:8082/getAPIEndPoint");

  BASE_API = endpoint.data;
  var axoisInstance = axios.create({
     baseURL: BASE_API,
     headers: {
      "Content-type": "application/json"
     }
  });

  return axoisInstance;
};

In ProductService.js file

import http from "../http-common";

class ProductDataService {
  getAll() {
    return http.get("/getAllRegisteredProducts");
  }

.....


Solution

  • Because you're exporting an async method you need to wait for it.

    const axiosInstance = await http
    return axiosInstance.get("/getAllRegisteredProducts");
    

    Of course you need to call this from an async context.