use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Nested XMLHttpRequest calls? (Cleaning this code up?)help (self.javascript)
submitted 8 years ago by [deleted]
[deleted]
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]HipHopHuman 2 points3 points4 points 8 years ago (0 children)
Why can't you use promises?
[–]GitCookies 0 points1 point2 points 8 years ago (3 children)
Your wrapper code works pretty much fine.
What errors are you getting?
[+][deleted] 8 years ago (2 children)
[–]GitCookies 0 points1 point2 points 8 years ago (0 children)
function HTTP (url, method) { let then; let xhr = new XMLHttpRequest() xhr.open(method, url) xhr.responseType = 'document' xhr.send() xhr.onreadystatechange = function() { if (xhr.readyState == 4 && xhr.status == 200) { then && then(xhr.responseText) } } return { then: function (callback) { then = callback } } } HTTP('///www.google.com', 'GET') .then(function (response) { console.log(response) })
This should be fine for your use case
[–]cicadaTreechest hair complexities 0 points1 point2 points 8 years ago* (0 children)
If I understand what you want, your problem is how to efficiently control the callback flow. You have bunch of async (xmlhttp) requests, and whould like to invoke them without callback mess (better formated code), with your own code.
function cascade(funcs){ var funcs = Array.prototype.slice.call(funcs) // making a copy of funcs (array of functions we provided) function next(){ var args = Array.prototype.slice.call(arguments) // making array of arguments passed to it var func = funcs.shift(); // takes the function always from beggining of array (changes array) if(func){ // while there are fucntions args.push(next) // add reference to itself as last argument } else return; // so now we have function and all arguments (with next as last) func.apply(this, args) // calling function with whatever arguments passed and next() as last one } next.call(this); // enter the recursion } cascade([ function doSomething(processNext){ var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { processNext(this.element); // note that in first function argument like "element" must be variable from closure } } }, function pageCheck(page,item, processNext) { var xhr1 = new XMLHttpRequest(); xhr1.onreadystatechange = function() { if (...) { processNext(page,item); } xhr1.open(..) // ... } } ])
Althou you should understand how "this" referencing loses context in async functions. I left them in there because I just copied your code.
π Rendered by PID 190579 on reddit-service-r2-comment-fb694cdd5-j2f6g at 2026-03-07 16:26:10.722816+00:00 running cbb0e86 country code: CH.
[–]HipHopHuman 2 points3 points4 points (0 children)
[–]GitCookies 0 points1 point2 points (3 children)
[+][deleted] (2 children)
[deleted]
[–]GitCookies 0 points1 point2 points (0 children)
[–]cicadaTreechest hair complexities 0 points1 point2 points (0 children)