Issue with variable's scope in GnomeJS

Hello,

I am trying to fetch a value from an API in a Cinnamon applet (GnomeJS-based).

In standard Javascript I would have used fetch(), but that does not seem to work in GnomeJS. So instead I am having the following, which kind of works, except that I can’t manage to use the fetched value in the parent function (getValue), where I get an empty object.

Would you have a clue for me ?

function getValue(API_URL) {
    try {
        const value = fetchValue(API_URL);
        global.log('afterFetch:', value); //empty object
        return value; 
    } catch (error) {
        global.warning('Can\'t fetch value', error);
    }
}

async function fetchValue(API_URL) { 
    const fetch  = await makeJsonHttpRequest({
        url: API_URL,
        headers: {'Cache-Control': 'no-cache'},
        onSuccess: (resp) => {
            const value = processResp(resp); //this outputs a string 
            global.log('fetching:', value); //It shows the correct value, but How to make it available in the parent calling function ?
            return value;
        },
        onErr: (err) => {
            global.logWarning(`Fetch failed."`);
        },
    });
    global.log('fetchResult:', fetch); //undefined
}

Here is the code for makeJsonHttpRequest() which is a handler for GnomeJS’ HTTP client/server library Soup.

Note: This topic was crossposted at https://forums.linuxmint.com/viewtopic.php?t=437312

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