-3

A similar question has been asked many other times, I'm aware, but my case is rather specific and has, to my knowledge, never been touched on before. At least from what I can find.

What I'm doing is building objects using a UID. Everything that the object is built with requires this UID and it requires it to be verified as unique before anything can be done with it.

I'm randomly generating the UID in javascript and I'm checking it against all other entries in a SQL database. I'm posting to the database using PHP and Ajax.

The core issue I'm having is that Javascript doesn't wait for the Ajax response and instead just keeps rolling. Ajax has to use a success handler. This is strictly not possible for me to use because I cannot do anything until I know for certain that the UID is verified.

Are there any workarounds or solutions to this? Promises won't work because, as I stated before, the UID is integral in building the object in and of itself, so using placeholders won't work.

Thanks in advance!

Kats
  • 143
  • 1
  • 12
  • If you are using jQuery have a look at the `async` setting of $.ajax (set it to `false`) - see also http://api.jquery.com/jQuery.ajax/ – Andreas Schrammel Oct 25 '16 at 21:14
  • 1
    so.... why cant you wait with creating an object until your first ajax call returns with success? You can kick the object creation method from that success callback, right? – Vladimir M Oct 25 '16 at 21:14
  • Looks like you're generating UID on client side and then verifying from server/db. Can't you generate a verified UID on the server and pass to the page when it loads the first time? – Talha Awan Oct 25 '16 at 21:18
  • @Kats wait, why wont promises work? u could say verify(UUID).then(dowhatever) , no? – Sajjan Sarkar Oct 25 '16 at 21:20
  • It doesn't matter where the UID is being generated from, it's the fact that I have to wait for it regardless. The objects being created are client-side and while I could theoretically just run the object creation code in a callback function, that would require a major rewrite of almost all of my object creation code in the first place and given that I'm asking questions on this website, I'm in a bit of a time crunch as-is. – Kats Oct 25 '16 at 21:20
  • Dupe: http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call – Marc B Oct 25 '16 at 21:24
  • [As of jQuery 1.8, the use of async: false with jqXHR ($.Deferred) is deprecated; you must use the success/error/complete callback options instead of the corresponding methods of the jqXHR object such as jqXHR.done().](http://api.jquery.com/jquery.ajax/) – Jay Blanchard Oct 25 '16 at 21:27

3 Answers3

1

You can just send a synchronous Ajax request, like that:

var response = $.ajax({ url : "http://api.jquery.com/jquery.ajax/", async : false });

(though that is not a very good practice)

zeppelin
  • 8,947
  • 2
  • 24
  • 30
  • That's what I was trying to avoid, was using `async:false`. I'm also aware that apparently async is deprecated in many browsers, which is another huge concern. Does async prevent _all_ Javascript from running or does it only prevent that particular stack from continuing? – Kats Oct 25 '16 at 21:17
  • yes it does, however that behaves like when a page is loading for the first time, and so everything will have to wait until success is returned! – Dennisrec Oct 25 '16 at 21:24
  • it locks the browser up, basically. that's why sync calls are highly depcrecated – Marc B Oct 25 '16 at 21:25
  • That's exactly the issue I'm trying to avoid. I'm much more versed in C++, so working with Javascript and asynchronous languages is fairly confusing. – Kats Oct 25 '16 at 21:26
  • >Does async prevent all Javascript from running or does it only prevent that >particular stack from continuing? Well, it blocks the execution of your current chunk of code, and as Javascript is single threaded (not considering the edge cases like WebWorkers and the things like that), no other Javascript code will run, until it completes. – zeppelin Oct 25 '16 at 21:27
  • [As of jQuery 1.8, the use of async: false with jqXHR ($.Deferred) is deprecated; you must use the success/error/complete callback options instead of the corresponding methods of the jqXHR object such as jqXHR.done().](http://api.jquery.com/jquery.ajax/) – Jay Blanchard Oct 25 '16 at 21:28
0

Ok. One possible solution, if your UID is generated on the server randomly, you may supply it directly to your index.html with the script tag.

<head>
<script src="path_to_url_that_provides_generated_script"></script>
<script .... other scripts></script>
</head>

And this generated script would be something like:

var UID = "xxxx-xxxx-xxxx-xxxx";

That way you don't even need to do an ajax request, since your uid will known before any other script is processed.

Vladimir M
  • 4,403
  • 1
  • 19
  • 24
0

The best method, I have discovered, was to rewrite my code to not begin object creation until I had the UID in the first place. While this does take up a considerable amount of time to complete and ensure that no unwieldy bugs are present, there's no other real solution that's both simple and effective.

As it turns out, foresight is something that always seems to keep good programmers from writing and rewriting their work again.

Special thanks to @Vladimir M for the solution.

Kats
  • 143
  • 1
  • 12