Synchronized NODEJS Code

Posted By : Himanshu Goyal | 28-Mar-2016

Synchronized NodeJS Code

Intro:The synchronize is nodejs npm module used make the node code synchronized and solve the problem of aysnc programming.it is oposite to the async module of npm and it can good for money relative senstive task.

Problem Facing:I am working on money calculation in MLM(Multi Level Marketing) using neo4j. I have lot of users and i write the code  asynchronized for all the user.At same time update hit goes to neo4j and courrept the data and make the calculation wrong.So I search on making the code synchronized in node js.According my reseach two way to make the code synchronized .

  • Recursive function 
  • Synchronized npm module

The recursive function is complex and make lot of effort to make the code synchronized. But synchronize npm is easy and efficent way to make the code synchronized.

Install Procedure:

  • npm install synchronize
  •  var calculateMoney= sync(readFileMethod);//put the reference name of method 

Code:

var sync = require('synchronize')

var read = function(a, callback) {
  setTimeout(function(){
    // remember that callbacks expect (err, result)
    callback(null, a)
  }, 1000)
}

// We can use the `sync` helper here to avoid using `await()` and `defer()` manually.
read = sync(read);

sync.fiber(function() {
  var results = []
  results.push(read(1)))
  results.push(read(2)))
  console.log(results)// results now contains [1,2]
})
 

Thanks

About Author

Author Image
Himanshu Goyal

Himanshu is a bright Java ,Mean stack developer and have good knowledge of Hibernate, Spring,FFmepg,Neo4j JSON, Jquery, NODE.JS .His hobbies are learning new things, fitness .

Request for Proposal

Name is required

Comment is required

Sending message..