Getting a promise back from server in hype with an iteration?

...and this may be more of a javascript hiccup than anything else. I have my code working in hype now, but I want to do this on an iteration based on the number of values coming in from SQL which I am hardcoding for y'all for this example. has anyone done anything like what I am attempting and if so can you point me in the right direction? so many times I find myself wanting to write iterative code that will eventually drive the format of and subsequent execution of javascript functions...

here you can see where I am modifying the standard request (xhr) and trying to adapt it to an array iteration xhr_[i] ---- if this syntax is off because of hype, please let me know. i am also bouncing between PHP and javascript a lot, perhaps this is where errors come in. i don't know if I need this syntax let xhr_ + [I] - that particular method seems to break things. and bonus question - who else uses PHP storm to write code for hype and has anyone tried webstorm with hype?

thanks for taking the time to look at this question - I appreciate all of you!

for (let i = 0; i <= numberOfDreams i++) {

    return new Promise((resolve, reject) => {
        let xhr_[i]                = new XMLHttpRequest();
        xhr_[i].onreadystatechange = function (evt) {
            if (xhr_[i].readyState == 4) {
                let res_[i] = JSON.parse(xhr_[i].responseText);
                console.log('res: ', res_[i]);
                console.log('status:', xhr_[i].status, ' response: ', xhr_[i].responseText);
                switch (xhr_[i].status) {
                    case 200:
                        let d = res_[i].data;
                        jwt   = d.jwt;
                        url   = d.urls[0];
        xhr_[i].open("POST", apiPath, true);
        xhr_[i].setRequestHeader("Content-Type", "application/json");
        xhr_[i].send(JSON.stringify({streamAccountId: accountId, dreamName: dreamName[i], unauthorizedSubscribe: true}));

} // end for loop

This might be a better question for a place like Stackoverflow or Sitepoint since it's not really Hype-ey ? While there might be someone here who knows the answer, those sites might be better for this type of Q...

1 Like

thank you! i will create an account on one of those and give this a shot there.

i did not fully read your complete post, but i can't see a wrapping function for your returnstatement ...

this link using promise.all may be helpful too:


Thank you all! I realized that some things don't need to be refactored and I left well enough alone, we have a few requests happening independently of each other, and it's working so no need to break it. for anyone reading this that is curious, I realized that since each callback is looked at separately, it may complicate matters too much to try to wrap that function call into an iterative statement since it comparatively over complex to figure out how to chunk the returning data. thank you to everyone who looked at this and made suggestions. Gosh, I don't say it enough - this is one of the friendliest forums around. It's appreciated!

1 Like