0
class monitor {
    constructor(){
        this.delay = config.delay

    delay(time) {
        return new Promise(function (resolve) {
            setTimeout(resolve, time);
        });
    }
        async redacted (pid) {
            if (this.err === true) {
                await this.delay(this.delay)
            }
            console.log("MONITOR > Getting Item Attrs")
            const options = {
                method: 'get', 
                url: url + pid + '.json', 
                headers: {
                    accept: '*/*',
                    'accept-encoding': 'gzip, deflate, br',
                },
                proxy: this.proxy
            }

            return req(options)
            .then((res) => {
                //console.log(res)
                let variants = res.data.skus
                //console.log(variants)
                const att = []
                for (let [key, value] of Object.entries(variants)) {
                    if(value.inStock) att.push(key)
                }
                if(att.length >= 1){
                    console("MONITOR > Sourced Item")
                    return att;
                } else {
                    ("MONITOR > No Variants Available")
                    this.oos = true
                    this.redacted(config.pid);
                }
            })
            .catch((err) => {
                if (err?.response?.status == 403) {
                    console.error("MONITOR > Proxy Block @ GET PRODUCT")
                    this.err = true
                    this.redacted(config.pid);
                }
            })
            
        }
}
var hello = new monitor().redacted(config.pid);
console.log(hello)

From what I understand I need to wait for the promise to finish before returning but I am confused on how to execute this with my code and also call this file and store the return value in another file. I'm not sure if my formatting is wrong as well, but I tried changing and no fix anyways

3
  • Try using this. stackoverflow.com/questions/14220321/… There are a lot of helpful soloution there. Commented Oct 24, 2022 at 16:56
  • store the return value in another file - Don't expect to be able to return the value from an async function to non-async code, it doesn't work like that. Your var hello will not contain anything useful when the console.log(hello) line runs. You would need to await redacted (only can do that from within async code) or provide a then function to run after redacted.
    – James
    Commented Oct 24, 2022 at 16:56
  • @james My other script is using async code as well but It is using multiple threads within the same class based on my config parameters so I wanted to implement a single "Task" in this case a monitor to push variants to an array to be used within another class x amount of times.
    – Steve Olla
    Commented Oct 24, 2022 at 17:09

1 Answer 1

-1

This snippet isn't using asynch but, it gives you the idea. You need to await or .then the promise (the fetch/request). You first fetch the remote JSON file then you parse it / read it. Something like:

    function getJSON(url) {
        return new Promise(function (resolve, reject) {
            var xhr = new XMLHttpRequest();
            xhr.open('get', url, true);
            //xhr.responseType = 'json';
            xhr.onload = function () {
                var status = xhr.status;
                if (status == 200) {
                    resolve(JSON.parse(xhr.response)); // <---------------
                } else {
                    reject(status);
                }
            };
            xhr.send();
        });
    };

    getJSON(primaryURL).then(function (res) {
        if (res) {
            //do something
        } else {
            console.log("response not found");
        }
    }).catch(function (error) {
        console.log("Error getting document:", error);
    });
7
  • Presumably the req function already does that.
    – Quentin
    Commented Oct 24, 2022 at 16:55
  • Right, well he must be forgetting to parse the file? Commented Oct 24, 2022 at 16:57
  • The question is very unclear. I think it might be yet-another "How can I get the resolved value of a promise instead of the promise?" question, but it lacks the usual "This is what I tried, this is the result I expected, this is the result I got" that would make it clear.
    – Quentin
    Commented Oct 24, 2022 at 17:03
  • @Quentin Okay so essentially I have another script that has another class running "tasks" x amount of times (Task count) but I need a singular function call (Task) for this so I am trying to figure out the easiest way to implement it which might just be calling it once outside of my taskcount loop.
    – Steve Olla
    Commented Oct 24, 2022 at 17:12
  • Steve in that case, task(){tasks()}; right? Or, if tasks is async then await resolve prior to moving on. Commented Oct 24, 2022 at 17:18

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.