For example, –
I have a Javascript code –
1st way of defining async function
import axios from "axios";
const fetchRepos = async (searchTerm) => {
const response = await axios.get(
`https://api.github.com/search/repositories?q=${searchTerm}`,
);
return response.data.items;
}
Here the fetchRepos
could be defined as a standalone async function –
2nd way of defining async function
async function fetchRepos(searchTerm) {
const response = await axios.get(
`https://api.github.com/search/repositories?q=${searchTerm}`,
);
return response.data.items;
}
What are the advantages or say disadvantages (if any), of defining functions in the first way?
I have recently published a npm package gitfm (work in progress) to fetch & render or clone (or both) a repo selected from a list of repos based on search input, inside your terminal as a CLI app. But I am now thinking to provide it also as a very simple library to be easily integrated with other node projects. I have defined different functions for different tasks. Which are executed in a final async function named main
. Then I called the main function at the bottom of the file. All the functions are defined in the 1st way (as named variables).
Also, when I will export the functions defined in the package, will it be bad or cause any issues when imported to be used in other projects if the functions are actually named variables?
Thank You.