JavaScript Javascript synchronous functions - chrome extension [duplicate] javascript synchronous functions,js synchronous functions,javascript synchronous callback,javascript

This question already has answers here:
Answer:1

Since you are using sync XHR, instead of ajax, you need to use a sync function to save data, instead of chrome.storage, which is async.

In the chrome.storage documentation, one of its features is

  • It's asynchronous with bulk read and write operations, and therefore faster than the blocking and serial localStorage API.

But being blocking (and sync) is what you want, so why don't you change to that API instead?

Or even better:

Convert your getTranslation() function to be async. You would only need to add a third parameter that would be a callback, and use it inside the nested callbacks (because if you do this, you can also use ajax instead of sync XHR).

That way is the right thing, but if you feel lazy and want an easier way, just do the former and change chrome.storage to localStorage and you are done.

EDIT: I see you have already changed you function to be async. And it seems it works correctly, but you updated your question and you seem to have problems grasping why this line does not work:

console.log(translated); //this is first and empty

You need to understand how event oriented programming works. You may think that the line

for (counter in toTranslateArray)

which contains getTranslation means "do translation of every counter inside this toTranslateArray", but actually means "fire a translation event for every counter inside this toTranslateArray".

That means when that console.log get executed this tasks have just been fired; it does not wait for it to be finished. Therefore translated is empty in that moment. And that's normal, is executed async.

I don't know what you need to do with the translated var once is finished, but I would try to fire a different event once the last item of the array gets processed.

But anyway, what you need is to do is to study some tutorial or something about event oriented programming so all this makes more sense to you.

About the localStorage, I don't know, I found out about that as an alternative in the chrome.storage documentation, I really don't know how to use it in your case.

But since javascript is event oriented, I really recommend you to learn events instead of just going back to sync. But is up to you.

Answer:2

I want to reload a template and the connected Controller without using $routeProvider, because my Path-structure doesn't fit with .when(...). I want to use $location.path() to read the URL and set the ...

I want to reload a template and the connected Controller without using $routeProvider, because my Path-structure doesn't fit with .when(...). I want to use $location.path() to read the URL and set the ...

  1. angular reload template
  2. angular 6 reload template
  3. angular directive reload template
  4. angular 2 reload template
  5. angular force reload template
  6. angular 2 reload component template
  7. angular 2 force template reload
  8. angularjs reload template

Currently having a problem in a app. My app is designed to open an RFID reader on a Raspberry PI. It proceeds to read the incoming RFID tags. The code is as follows; // Socket.io server details var ...

Currently having a problem in a app. My app is designed to open an RFID reader on a Raspberry PI. It proceeds to read the incoming RFID tags. The code is as follows; // Socket.io server details var ...

I am calling the Javascript window.open() function to load another url in a pop up window. When the users closes the popup window, I want the MyThanks() function to be called. How I can do this? My ...

I am calling the Javascript window.open() function to load another url in a pop up window. When the users closes the popup window, I want the MyThanks() function to be called. How I can do this? My ...

  1. call function when state changes react
  2. call function when input value changes
  3. call function when enter key is pressed
  4. call function when modal is closed
  5. call function when variable changes angular
  6. call function when element is visible
  7. call function when redux state changes
  8. call function when radio button selected
  9. call function when button is clicked javascript
  10. call function when checkbox is checked in javascript
  11. call function when props change react
  12. call function when checkbox is checked in jquery
  13. call function when page loads javascript
  14. call function when document is ready
  15. call function when submit form
  16. call function when div loads
  17. call function when page loads jquery
  18. call function when props change vue
  19. call function when dom is ready
  20. call function when observable changes

Let's say I have an HTML page, served up from example.com. It makes an javascript ajax request to targetServer.com Which server needs to return a Access-Control-Allow-Origin="(something)"? Is it the ...

Let's say I have an HTML page, served up from example.com. It makes an javascript ajax request to targetServer.com Which server needs to return a Access-Control-Allow-Origin="(something)"? Is it the ...

  1. server that needs staff
  2. who needs server
  3. server that needs help