Can JavaScript execute two asynchronous functions or callbacks at the same time? Or does it execute the first one and wait until it is finished then executes the other?
For example, let's say I made two requests and the two returns the response at the same time will JavaScript executes the two functions handleResponse1
and handleResponse2
at the same time.
async function handleResponse1 () {/* some code */}
async function handleResponse2 () {/* some code */}
fetch('https://github.com').then(handleResponse1);
fetch('https://stackoverflow.com').then(handleResponse2);
Another example is if my express server receives three requests for three different endpoints (/users
, /courses
and /products
) at the same time will JavaScript executes getUsers
, getCourses
and getProducts
at the same time.
async function getUsers () {/* some code*/}
async function getCourses () {/* some code*/}
async function getProducts () {/* some code*/}
app.get('/users', getUsers);
app.get('/courses', getCourses);
app.get('/products', getProducts);
The last example is if I read a file and Nodejs returns its data at the same that my express server receives a request will JavaScript execute processFileData
and processRequestData
at the same time.
async function processFileData () {/*some code*/}
async function processRequestData () {/*some code*/}
fs.readFile('/test.txt', 'utf8' , processFileData)
app.get('/some-endpoint', processRequestData);