http - Synchronous Blocks in Node.js -


i want know if it's possible have synchronous blocks in node.js application. i'm total newbie node, couldn't find answers behavior i'm looking specifically.

my actual application little more involved, want support request , post request. post request append item array stored on server , sort array. request return array. obviously, multiple gets , posts happening simultaneously, need correctness guarantee. here's simple example of code theoretically like:

var arr = [];  app.get(/*url 1*/, function (req, res) {     res.json(arr); });  app.post(/*url 2*/, function (req, res) {     var itemtoadd = req.body.item;     arr.push(itemtoadd);     arr.sort();     res.status(200); }); 

what i'm worried request returning array before sorted after item appended or, worse, returning array as it's being sorted. in language java use readwritelock. i've read node's asynchrony, doesn't appear arr accessed in way preserves behavior, i'd love proven wrong.

if it's not possible modify code have support behavior, there other alternatives or workarounds application want do?

what i'm worried request returning array before sorted after item appended or, worse, returning array it's being sorted.

in case of code here, don't have worry (although read on because may want worry other things!). node.js single-threaded each function run in entirety before returning control event loop. because array manipulation synchronous, app wait until array manipulation done before answering get request.

one thing watch out then, of course, if (for example) .sort() takes long time. if does, server block while going on. , why people tend favor asynchronous operations instead. if array guaranteed small and/or app limited number of users (say, it's intranet app small company), you're doing may work fine.

to understanding of whole single-threaded + event loop thing, recommend philip roberts's talk on event loop.


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -