Home > database >  Array length doesn't change in JavaScript
Array length doesn't change in JavaScript

Time:09-04

hey so i use express and everytime a req is made i don't respond to it and store it in an array (to long poll)

like this: { req, res }

but if i try multiple tabs the length stays 1 until one is responded to why does this happen? like, i could have 5 tabs (and realistically 5 reqs in the array) but only when i respond to one, another one appears (until all are answered)

code:

const reqs = [];

app.get("/", (req, res) => {
  reqs.push({ "req": req, "res": res });
  console.log(reqs.length); // 0 if none; but only 1 no matter how many requests are pending a response
});

i tried looping the reqs too using different for loops but only 1 responds per run of the function (manually ran) it's weird it's like node doesn't know the other reqs exist is it because the array get's too big orrr?

CodePudding user response:

but if i try multiple tabs the length stays 1 until one is responded to why does this happen? like, i could have 5 tabs (and realistically 5 reqs in the array) but only when i respond to one, another one appears (until all are answered)

This behavior sounds like the browser is withholding sending the subsequent requests until the previous one has responded. This is because these are identical requests to the same host, same URL and might be cacheable responses. You can see how the browser would benefit if three different parts of the page all requested the same image. The browser sends one request, then waits for it before deciding what to do with the other identical requests. If the response comes back and is cacheable, then the browser can just use that response to handle the subsequent requests, saving time and resources.

You can bypass this behavior by adding a variable query string to the URL (something that is different for each request) or by setting the right things on the request to say you don't want caching (an option for the fetch() interface for example).

For example, if each subsequent request is different:

/?i=1
/?i=2
/?i=3

Then, the browser will send all three without waiting for the first one to respond before sending the second one.

Keep in mind that a browser also has a limit on how many simultaneous requests can be in flight to the same host so you may also eventually run into that limit, but that value is typically something like 6, not 1 as you are experiencing.

CodePudding user response:

The only way this could be happening is if you have some additional code that is making you never reach your reqs.push

CodePudding user response:

You are sending req and res variables in a single object to reqs which means that reqs has only one object in it which is {"req": $req, "res": $res}.

If u want to provide multiple objects, Pls try in this way

const express = require('express')

const reqs = [];

const app = express()

app.get("/", (req, res) => {
  reqs.push( {"req": req});
  reqs.push( {"res": res });
  res.send("Hello World")
  console.log(reqs.length); // Will gives u 2 in this case as I have sent 2 objects into reqs , rather than one
});

app.listen(3000)

Hope, this is helpful to you.

  • Related