简体   繁体   中英

Is javascript asynchronous function internally designed using settimeout, setintervals etc?

Is javascript asynchronous function internally designed using settimeout function?

since javscript language is based on single threaded, it depends on the event-loop which is supported by the browser or task queue or something to create asynchronous function.

so is settimeout function only the way to make asynchronous function in javascript?

If you want to take advantage of separate processes you can use Web Workers ( https://developer.mozilla.org/en-US/docs/Web/API/Worker ) if they are supported by your target browsers.

IE:

var myWorker = new Worker('worker.js');
var first = document.querySelector('#number1');
var second = document.querySelector('#number2');

first.onchange = function() {
  myWorker.postMessage([first.value,second.value]);
  console.log('Message posted to worker');
}

Another possibility is to use function* like redux-saga does.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM