Answer the question
In order to leave comments, you need to log in
How to make a function asynchronous in JS?
I study at my leisure Node.JS on the site nodebeginner.ru
One of the points is the implementation of the server response in the "how to" and "how not to" options . I will not give the original code, I will post mine:
app.js (starter)
// modules
var server = require('server');
var router = require('router');
var requestHandlers = require('requestHandlers');
var handle = {};
handle["/"] = requestHandlers.index;
handle["/index.htm"] = requestHandlers.index;
handle["/dir"] = requestHandlers.dir;
handle["/upload"] = requestHandlers.upload;
server.start(router.route, handle);
var http = require("http");
var url = require("url");
function start(route, handle) {
function onRequest(request, response) {
var pathname = url.parse(request.url).pathname;
// console.log("Request for " + pathname + " received.");
route(pathname, handle, response);
// response.writeHead(200, {"Content-Type": "text/plain"});
// response.write("Hello World");
// response.end();
}
http.createServer(onRequest).listen(1337);
console.log("Server has started.");
}
exports.start = start;
function route(pathname, handle, response) {
// console.log("About to route a request for " + pathname);
if (typeof handle[pathname] === 'function') {
return(handle[pathname](response));
} else {
response.writeHead(404, {"Content-Type": "text/plain"});
response.write("File not found.");
console.log("No request handler found for " + pathname);
}
}
exports.route = route;
function sleep(callback, milliSeconds) {
var startTime = new Date().getTime();
while (new Date().getTime() < startTime + milliSeconds);
callback();
}
function index(response) {
// console.log("Request handler 'index' was called.");
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Request handler 'index' was called.");
response.end();
}
function dir(response) {
// console.log("Request handler 'index' was called.");
sleep(function(){
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Request handler 'dir' was called.");
response.end();
}, 10000);
}
function upload(response) {
// console.log("Request handler 'upload' was called.");
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Request handler 'upload' was called.");
response.end();
}
exports.index = index;
exports.dir = dir;
exports.upload = upload;
Answer the question
In order to leave comments, you need to log in
Alexey Ten: advised to use setTimeout
I remember myself a couple of years ago when I asked the same question.
To answer it, you need to understand how the event loop works.
All functions are synchronous, absolutely everything, but if a function takes too long to execute, it will become a blocking operation.
For example, you are reading a file, if you are reading it all at once, while it is being read, the entire stream of events is frozen in place.
If you read the file asynchronously, then everything happens exactly the same, but the file is read in small pieces, reading one piece is one function and each subsequent function is triggered by an event, and in between events for reading the file, the node will insert events to run other functions.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question