How to return this variable in fetch

Line 310
There is a way to return and cousume this data (data.content_type_id) or variable and make it accesible from other side?
O can I do that and how can a use it?
Thanks in advance.

return data from the last then block so that you can store it as the output of the function.

      contentTypeId = data.content_type_id
      return contentTypeId

Then in orher block I try calling that Fetch function as:
console.log(loadInitialData())

undefined.

And if i try calling contentTypeId in other block there is an error
contentTypeId is not defined

You should also add a return statement before the fetch

return a promise

Can you please explain your usecase. May be you can use async await instead of then catch

Please do not post images of code, but the actual code. Also, we have no idea what your API data looks like.


You can pass the data to a function, you can store it in some state, you can return the promise and call .then or await at the call site. There are many ways to do it. But I wouldn’t suggest saving it on the window object unless you have a really good reason to do so.

I would suggest you move your DOM code out of the fetch function and pass the data to functions that use the data.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.