I have two files: getPopular.js
and getPlayListByBPM.js
. I am importing function from getPopular.js
to getPlayListByBPM.js
.
In getPopular.js
, I wrote two async functions:
var getPopularArtists = async () => {
//https://nodejs.org/api/http.html#http_http_request_options_callback
request.get(options, function(error, response, body) {
return getArtistNameIdMap(body, {});
})
}
var getPopularTracks = async () => {
request.get(options, function(error, response, body) {
return getTrackIdList(body, {});
})
}
module.exports = {
GetPopularArtists: getPopularArtists,
GetPopularTracks: getPopularTracks,
}
with getArtistNameIdMap
and getTrackIdList
non-async functions.
In getPlayListByBPM.js
, I import the functions like this:
const GetPopular = require('./getPopular');
var popularArtists = await GetPopular.GetPopularArtists();
var getPlaylistsByBPM = (req, res) => {
console.log(popularArtists);
res.send("This is my BPM" + req.params.BPM);
}
While it gives me:
var popularArtists = await GetPopular.GetPopularArtists();
^^^^^
SyntaxError: await is only valid in async function
I would like to know how can should I call getPopularArtists
function when having them imported into another JS file?