Requirements
I want to use a separate file for the process to access the API
I want to use async / await
I want to use axios
The code below will be undefined
- test.js
import fetchApi from '../lib/store';
export default function() {
async function call() {
const res = await fetchApi();
console.log(res);//undefined
}
call();
}
- lib/store.js
import axios from "axios"
export default function() {
async function fetchApi() {
try {
const response = await axios.get('https://jsonplaceholder.typicode.com/todos/1');
return response;
} catch (error) {
return error;
}
}
}