0

My site has an admin panel that sends a lot of requests to the server. Now they are scattered across different components, which makes it very difficult to edit them. I want to collect them all in one js file, where the functions that will make post requests to the server via fetch. I ran into the problem of returning a value from a similar function due to fetch asynchrony. How do I return the received data from the server so that the function call with the request looks like this:

let sectionsList = get_sections_list ();

?

I tried:

function get_sections_list () {
    var data;
    const request = (callback) => 
        fetch ("/admin/sections/list", {method: "POST"})
            .then ((response) => response.json ())
            .then ((result) => callback (result))
    request ((answer) => data = answer);
    return data;
}

let sectionsList = get_sections_list ();

but this did not give the desired result.

0 Answers0