Recently, I had to fix security issues in a web application (which I did not create). A security issue was the use of cookies other than http. So I had to set only the http-only session-cookie, which means you can no longer read (and set) the cookie value from javascript. So far it has been easy.
The deeper problem was that the web application was using
JSON.parse(readCookie(cookieName)).some_value
per million places .
So, in order not to rewrite the “million lines of code”, I had to create an ajax-end point that gave me the http cookie content as JSON and rewrite readCookie to use SYNCHRONOUS ajax requests (instead of reading the cookie), because the rest of the awful code expects readCookie will be synchronous in these millions of places because reading a cookie is synchronous.
The problem is that I get a lot
The synchronous XMLHttpRequest in the main thread is deprecated due to its detrimental effects on the end user. For more help, check out https://xhr.spec.whatwg.org/ .
which spam the debug console, not to mention that someone decides to remove this functionality.
Therefore, I am studying the new async / await ES keywords to find out if this can somehow help in creating an asynchronous ajax request synchronously (I know that I need to use shells for IE 11).
So far i have read these pages
https://www.twilio.com/blog/2015/10/asyncawait-the-hero-javascript-deserved.html
https://pouchdb.com/2015/03/05/taming-the-async-beast-with-es7.html
https://jakearchibald.com/2014/es7-async-functions/
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function *
but it seems that all new asynchronous things, apparently, only make the task of writing asynchronous code easier, and do not allow interaction between asynchronous and existing synchronous code. Using the information I read, now I can wait for the result of an asynchronous ajax call, as if it were synchronous, but the problem is that - waiting is allowed only in asynchronous methods ... This means that even if I can wait for the result , as if it were synchronous, the getCookie method still had to be asynchronous, which makes all things completely pointless (unless all your code is asynchronous, which is certainly not when you are not starting from scratch) ...
I cannot find any information on how to interact between synchronous and asynchronous code.
For example, in C #, I can call an asynchronous method from a synchronous context with .Result, for example.
AsyncContext.RunTask(MyAsyncMethod).Result;
or simpler but less secure like
MyAsyncMethod(args).Result;
Is there a way to do the same in JavaScript?
It doesn't seem to make sense to propagate the asynchronous move when the rest of the code is synchronous, without any interoperability ... Is there really still no way to achieve this in JavaScript in 2017?
I emphasize again :
I know how I can make a synchronous ajax call, and I know how to use async async calls with callbacks and / or promises.
But I can’t understand how to synchronize async-ajax-call (without a callback) , so it can be used from code that is waiting to be executed synchronously (in "millions of places")!
This is what I have tried so far:
(Note that if I use loadQuote or main , the text "Ron once said" still appears first in the debug console, which should not be the case if the ajax call is asynchronous allowed synchronously )
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" lang="en"> <head> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" /> <meta http-equiv="cache-control" content="max-age=0" /> <meta http-equiv="cache-control" content="no-cache" /> <meta http-equiv="expires" content="0" /> <meta http-equiv="expires" content="Tue, 01 Jan 1980 1:00:00 GMT" /> <meta http-equiv="pragma" content="no-cache" /> <meta charset="utf-8" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta http-equiv="Content-Language" content="en" /> <meta name="viewport" content="width=device-width,initial-scale=1" /> <meta name="google" value="notranslate" /> <title>Title</title> <style type="text/css" media="all"> body { background-color: #0c70b4; color: #546775; font: normal 400 18px "PT Sans", sans-serif; -webkit-font-smoothing: antialiased; } </style> <script type="text/javascript"> </script> </head> <body> <script type="text/javascript"> function getQuote() { var quote; return new Promise(function (resolve, reject) { ajax.get("./ajax/json.ashx", null, function (bError, strMessage, iStatus) { </script> </body> </html>