-3

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);
  • Async functions _always_ return promises. – jonrsharpe May 05 '21 at 09:08
  • Does this answer your question? [Async function returning promise, instead of value](https://stackoverflow.com/questions/51338277/async-function-returning-promise-instead-of-value) – Ivar May 05 '21 at 09:10
  • And you should use `response.json()` if you want an object. – Thomas May 05 '21 at 09:12
  • I have updated the code with the modifications suggested however the error still persists – starter-kid May 05 '21 at 09:16
  • [It should work fine](https://jsfiddle.net/ojpcde4z/2/) the way you edited it. – Ivar May 05 '21 at 09:21
  • Can you show the updated code. Because it is not possible that you get a Promise out of `await` and it is not possible to get something else out of an `async function`. – Thomas May 05 '21 at 09:22
  • This is the same code I use, It's just encapsulated in another function since I call this function in other JS files but when I call it from other JS files it gives me object promise – starter-kid May 05 '21 at 09:27
  • 1
    Your outer function is just as async as the inner one is. Again, `async` functions _always_ return a Promise, never a value. Please read the accepted answer in the post linked in my first comment. You'll need to `await` `script.getSub(1)` as well. (Or use `.then()` on it.) – Ivar May 05 '21 at 09:34
  • I updated the code above to better show what i was trying to do. I tried to use the settimeout but still no luck – starter-kid May 05 '21 at 09:34
  • @Ivar I'm getting another error when I placed await beside script.getSub(1) : 'Uncaught SyntaxError: await is a reserved identifier' – starter-kid May 05 '21 at 09:37
  • @starter-kid Then you likely put the `await` in the wrong spot, or the `script.getSub(1)` is not inside of an `async` function (although in that case I would expect a different error). Do you have it like `const info = await script.getSub(1);`? – Ivar May 05 '21 at 09:40
  • yep like this const info = await script.getSub(1); – starter-kid May 05 '21 at 09:42

1 Answers1

-1

You should use await when calling an async function:

function getSubject(courseid) {
    var url = 'http://localhost:1337/subjects/'+courseid;
    return fetch(url, {method: 'GET'});
}

var subject = await getSubject(1);
console.log(subject)
Karma Blackshaw
  • 880
  • 8
  • 20