i'm trying to fetch data from an API and I tried following this tutorial but it keeps returning [Object Promise] instead of the object with the course id. The fetch function works properly when I tested it in the console but it doesn't set the variable 'subject' to the returned data. Is there any parts that need to be modified? Thank you
localhost:1337/subjects/
[{
"id": 1,
"name": algebra",
"time": "TUES|2PM",
"instructor": "Alex Smith"
},
{
"id": 2,
"name": "biology",
"time": "WED|2PM",
"instructor": "Carl James"
}]
script.js
async function getSub(courseid) {
async function getSubject(courseid) {
var url = 'http://localhost:1337/subjects/'+courseid;
let response = await fetch(url, {method: 'GET'});
let data = await response.json();
return data;
}
const subject = await getSubject(1); //returns [object Promise] not object with id=1
}
calling this function from another js file
const info = await script.getSub(1);