Skip to content

Instantly share code, notes, and snippets.

@eMahtab
Created July 7, 2017 18:15
Show Gist options
  • Save eMahtab/d61430fb142206de6d0ffb21bcd6c15b to your computer and use it in GitHub Desktop.
Save eMahtab/d61430fb142206de6d0ffb21bcd6c15b to your computer and use it in GitHub Desktop.
Yes Array.forEach is synchronous but be careful when you write async code inside forEach function body.
var fs = require('fs');
fs.readFile('users.json',function(err,data){
var users = JSON.parse(data).results.slice(0,100);
console.log("Users "+users.length);
users.forEach(function(user,index){
var prefix = index+1;
fs.writeFile('users/user_'+prefix+'.json',JSON.stringify(user),function(err){
if(err){
console.log("Error "+err)
}
else{
console.log(prefix + " writing done")
}
})
console.log(prefix + " done")
})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment