The most important thing hasn't been explicitly described here: Long polling is implemented server-side.
That's why the Javascript code for long polling and polling looks about the same - the client doesn't really do anything different. The actual implementation of "Does this endpoint respond immediately or wait until it has a non-empty response?" is server-side code that resides on your webserver.
Your server is the one that decides when to respond. If it decides it may sometimes wait, then it's a long poll.
Endpoint code that makes some changes in-memory and returns is a regular poll.
Endpoint code that makes a slow SQL query and returns right after is a regular poll.
Endpoint code that checks a message queue, sees it's empty, sleeps the thread a bit, and checks it again until the queue has something is a long poll.
The client just waits in any case.
In "regular" polling, the response often comes back quickly - determined by latency and how long the server needs to process the request.
In "long" polling, the response may still come back quickly, if the server only needs to wait briefly until it has something to respond with. But it may also take a while, because the server is willing to procrastinate its response so that it can respond with something useful.
From the client's perspective, it's basically the same. It sends a request, and at some point it gets a response. With long-polling it may be a little longer, but not necessarily.
Note that connections may timeout, so the example code wouldn't be used in production. You'd want some handling for timeouts and error responses, usually just retrying the request.