Posts

Showing posts with the label asynchronous

How do I run a nested loop synchronously?

How do I run a nested loop synchronously? I'm writing a simple script that prints text to the screen one character at a time. I'm making it so that the function (which I've called slowPrint ) can receive an Array of Strings. Each element in the Array represents a message. slowPrint This is the code I have so far: However, I am not getting the expected output. I suspect this is in part to the asynchronous nature of the code, though I don't have a full and clear understanding of what's happening and how to fix it. To begin with, the <br /> tags are being printed before any of the messages, which tells me that the outer loop is finishing before the nested one even starts. <br /> When the nested loops does begin, however, each string in the array is being printed one second apart, but in their entirety rather than character by character . What am I missing? Additionally, can someone please explain the following behavior of the setTimeout method? setTimeout...

When connection to facebook is slowed down the site loads slowly

When connection to facebook is slowed down the site loads slowly We have a public website. It contains the Facebook Pixel script. However, in our region sometimes the speed to the Facebook drops down dramatically whereas the rest of the Internet works fine. When it happens the main page loads very slowly. This is the script located in the head of the page: As far as I know this script should load asynchronously but it seems that is doesn't, because the page cannot render completely until the facebook pixel finishes its work. I even set the pagespeed EnableFilters defer_javascript in the pagespeed module of my nginx but it doesn't help. pagespeed EnableFilters defer_javascript What can I do? What am I doing wrong? Try adding <script async> in your tag. P.S only works on external scripts using src . – Alex Jun 29 at 11:02 ...

Node request external API within local get request

Node request external API within local get request I'm trying provide the response of an external API call when I perform a local GET request but struggling with how to get this to work. My code at the moment is: app.get('/', function(req, res){ request.post('http://data.fixer.io/api/latest?access_key=' + apikey + '&symbols=gbp', function(err, res, body) { console.log(body) }) res.render('index') }) My knowledge and experience with callbacks and async programming is limited, but how do I pass the response of the request POST into the GET request to then pass it to the index? Thanks! 2 Answers 2 Callbacks can be difficult to understand, and the problem you describe isn't uncommon (it even has a name - Callback Hell). Partly the reason why Node introduced the async / await syntax - here's the equivalent of your code in that style async await ...

GAE Standard Async Fetch not working

GAE Standard Async Fetch not working I'm following the docs and yet it appears the requests are still being made synchronously. https://cloud.google.com/appengine/docs/standard/python/issue-requests Here is my code: rpcs = for url in urls: rpc = urlfetch.create_rpc() urlfetch.make_fetch_call(rpc, url) rpcs.append(rpc) result = for rpc in rpcs: result.append(rpc.get_result().content) return result I did some profiling and compared using requests.get and they both take exactly the same amount of time. requests.get The urls i'm fetching are from different sites so I'm sure that I don't have concurrent limitations on the server side. Running on GAE Standard, Python 2.7 I followed the docs myself and can confirm that the calls are made asynchronously. Why you think requests are still synchronous? I believe that you are missing the point here. Difference between async and sync is that sync request blocks the client until operation...