Tag: asynchronous

Being both event-driven servers, why node.js needs async code where Nginx doesn't?

The question is in the title. In another words, if Nginx works as the same event-driven async IO model of node.js, why doesn’t it requires writing async style code? I know, Nginx is NOT actually executing any code, rather proxying them to who can. Then why doesn’t node do so? Are we missing anything in […]

How to re-use async callback function?

I am using async.parallel in two functions with same callback function . In second function (two()) added third function as addition function function one(){ var testuser = 1; async.parallel([onefunction, secondfunction], function(err, result){…}) } function two(){ var testuser = 2; async.parallel([onefunction, secondfunction, thirdfunction], function(err, result){…}) } function onefunction(callback){….code with testuser……} function twofunction(callback){….code with testuser……} function thirdfunction(callback){….code […]

Not all documents are inserted in MongoDB when using the async-driver for Java

I was experimenting with the mongodb-async driver(http://mongodb.github.io/mongo-java-driver/3.0/driver-async/) and noticed odd behaviour. I reproduced the weird behaviour in underlying code: import com.mongodb.async.SingleResultCallback; import com.mongodb.async.client.MongoClient; import com.mongodb.async.client.MongoClients; import com.mongodb.async.client.MongoCollection; import com.mongodb.async.client.MongoDatabase; import org.bson.Document; public class main { public static void main(String [] args) { MongoClient mongoClient = MongoClients.create(); MongoDatabase database = mongoClient.getDatabase(“mongotest”); MongoCollection<Document> collection = database.getCollection(“coll”); for(Integer […]

Managing connections on a command based TCP socket API in node.js

I built a RESTful API based on expresss.js which communicates to a remote server through a TCP socket using JSON. Requested URLs are converted into the appropriate JSON messages, a new TCP socket is open and the message is sent. Then when a message coming from the same connection is received an event is fired, […]

How the Node.js async eachLimit works in this situation?

I wrote a little async script to batch insert a lot of JSON files into a MongoDB sharded cluster. This is my first time with this module (and I’m still learning Node.js). I don’t know if I’m doing it right. The code is the last part of a waterfall (1): previous functions end up with […]

How to Write an Asynchronous While Loop in Node.JS

I am writing a node.js app for to help automate some of my home brewery. One of the modules I am using is a PID algorithm to control outputs so that they maintain certain setpoints. I am currently doing this via a while loop, but am thinking that this code will be blocking. Any help […]

In Node.js, how do I create a sha512 hash asynchronously?

var crypto = require(‘crypto’); var sha = crypto.createHash(‘sha512’).update(String(s)); var result = sha.digest(‘hex’); That’s my current code. How do I do this async? I’m planning to do the sha512 100,000 times.

How to perform multiple queries all at once in sails.js

For example, I have a model Wallet. And I need to perform several actions. Let’s say: Async.waterfall([ Wallet.findOne(criteria1).exec(cb1); Wallet.update(criteria2).exec(cb2); Wallet.findOne(criteria3).exec(cb3); Wallet.update(criteria4).exec(cb4); ], …); Not exactly right syntax for ASync, no matter. As I understand it is going to be four connections to database server one by one. Is it possible to perform all actions by […]

Trouble to synchronise promises in Node.js using Q

I am currently doing an API in Node.JS with the framework Sails.js. I am using promises for the first time and I have some troubles to sync my promises like I want. My main function is the following : createCard: function(req, res) { checkIfUserHasStripeAccount(req.user) .then(addCreditCardToStripeAccount()) .then(function cardCreated() { res.send(200, { msg: ‘Card created’ }); }) […]

How should I batch upload to s3 and insert to MongoDB from nodeJS webserver with a final callback?

I have a web server that accepts images from client, processes them, upload them to S3, batch-insert the urls to my mongoDB, and lastly sending the json result back to the client. Working with a single image works as follows: router.post(“/upload”, function(req, res){ var form = new multiparty.Form(); form.parse(req,function(err,fields,files){ s3.upload({ Key: filename, Bucket: bucketname, ACL: […]

Node.js is the Best Javascript runtime in the world.