filesize_done = 0;
path.exists(filename + "." + 0, function(exists) {
fs.stat(filename + "." + 0, function(err, stat) {
filesize_done += stat.size;
path.exists(filename + "." + 1, function(exists) {
fs.stat(filename + "." + 1, function(err, stat) {
filesize_done += stat.size;
path.exists(filename + "." + 2, function(exists) {
fs.stat(filename + "." + 2, function(err, stat) {
filesize_done += stat.size;
console.log(filesize_done);
});
});
});
});
});
});
Refactorings
No refactoring yet !
Tim Caswell
August 26, 2010, August 26, 2010 00:04, permalink
Use Step and path.exists is redundant when you're doing a stat anyway.
// http://github.com/creationix/step
var Step = require('step'),
fs = require('fs');
// Simple utility that doesn't error out when the file doesn't exist
// and returns the size
function getSize(filename, callback) {
fs.stat(filename, function (err, stat) {
if (err) {
if (err.errno === process.ENOENT) {
return callback(null, 0);
}
return callback(err);
}
callback(null, stat.size);
});
}
var getTotal = Step.fn(
function startChecks(filename, num) {
var group = this.group();
for (var i = 0; i < num; i++) {
getSize(filename + "." + i, this);)
}
},
function combine(err, sizes) {
if (err) throw err;
var total = 0;
sizes.forEach(function (size) {
total += size;
});
return total;
}
);
getTotal("Myfile", 3, function (err, total) {
// You now have the total size
});
Fabian Jakobs
August 27, 2010, August 27, 2010 07:06, permalink
With async.js you can rewrite your code in this functional style.
// http://github.com/fjakobs/async.js
var async = require("async");
async.files([filename + "." + 0, filename + "." + 1, filename + "." + 2])
.exists()
.filter(function(file) {
return file.exists;
})
.stat()
.reduce(function(file, sum) {
return sum + file.stat.size;
}, 0)
.end(function(err, sum) {
console.log(sum)
})
Shimon Doodkin
September 30, 2010, September 30, 2010 13:31, permalink
my new node-inflow library is really good it makes non-common staff more common.
http://github.com/shimondoodkin/node-inflow
//inflow.each( shared_object, array_or_object, foreach_function(value,key,array), done_function [, debug=true/false] );
var inflow = require('node-inflow');
var size=0;
inflow.each({},['file1.txt','file2.txt','file3.txt'],function (value,key,array){
var next=this.next;
var filename=value;
path.exists(filename, function(exists)
{
fs.stat(filename, function(err, stat)
{
size += stat.size;
next();
});
});
},function (){
console.log('size:'+size);
});
or
var inflow = require('node-inflow');
inflow.each({size:0},['file1.txt','file2.txt','file3.txt'],function (filename){
var next=this;
var shared=this.shared;
path.exists(filename, function(exists){
fs.stat(filename, function(err, stat){
shared.size += stat.size;
next();
});
});
},function (){
console.log('size:'+this.shared.size);
});
mablePabure
May 3, 2011, May 03, 2011 15:02, permalink
Medication Safety Quiz For Nurses http://www.champion-audi.com/ - prednisone medication It can also be used in the treatment of migraines, cluster headaches, leukemia, Hodgkin?s lymphoma, and hormone sensitive tumors. http://www.champion-audi.com/ - prednisone medication
encalryinsise
May 15, 2011, May 15, 2011 10:50, permalink
Immortality Pills
http://www.pgntravelteam.com/ - librium sale
It is an important drug as it was the first benzodiazepine to be synthesized.
http://www.pgntravelteam.com/ - librium sale
CafDahsmeashy
June 15, 2011, June 15, 2011 20:33, permalink
$4 Prescription Drug List Kroger http://crueton.com/ - cialis comprar Si usted es afectado de anemia, leucemia o cA?ncer de hueso, podrAa verse afectado por incomodas erecciones muy prolongadas, misma precauciA?n que deberA? tener si padece de problemas al hAgado, riA±A?n, enfermedades al corazA?n y anormalidades fAsicas en el pene, como curvatura muy pronunciada, cicatrices, etc. http://crueton.com/ - comprar cialis online
Elaravianna
July 18, 2011, July 18, 2011 01:36, permalink
Tablets Voltarol http://www.dunedinhomesforsale.org/ - cheapest librium The discovery of Lubrium was an accident, however, many ill patients are glad that it was discovered. http://www.dunedinhomesforsale.org/ - librium sale
poophycle
August 13, 2011, August 13, 2011 14:11, permalink
The Top Penis Enlargemnet Pills http://www.texashighschoolfootballonline.com/ - discount soma The first one among them is that soma drug is colorless. http://www.texashighschoolfootballonline.com/ - soma pills
Haraavaithser
August 27, 2011, August 27, 2011 02:04, permalink
Vibrating Pills http://www.gptim.org/ - xanax without rx Today it is used to treat depression related anxiety as well as a broad variety of anxiety disorders. http://www.gptim.org/ - xanax price
HerselsAborgo
August 18, 2011, August 18, 2011 14:50, permalink
Hdl Raising Drugs
http://www.hobinator.com/ - generic lasix no prescription
Lasix is used to prevent bleeding during a race in race horses.
http://www.hobinator.com/ - furosemide without prescription
occateNit
September 8, 2011, September 08, 2011 16:26, permalink
Hello, http://www.c16mag.com/ - klonopin pills The United States Food and Drug Administration approved Klonopin and it has been on the market since 1997. http://www.c16mag.com/ - generic klonopin
ingerfirm
September 15, 2011, September 15, 2011 11:56, permalink
jeogrargy http://www.ryanfinnoceanracing.com/ - cheap klonopin no prescription Patients as well as medical professionals prefer Klonopin because it has a very low toxicity, a short half life, and it also begins to work quite quickly. http://www.ryanfinnoceanracing.com/ - klonopin without prescriptions
NurnAeronse
October 9, 2011, October 09, 2011 10:17, permalink
Canine Pain Pills http://pedalforwishes.org/ - accutane sale Accutane is a medication that was originally developed as a chemotherapy type medication for the treatment of brain and pancreatic cancer. http://pedalforwishes.org/ - buying accutane online
Querdorie
October 7, 2011, October 07, 2011 12:17, permalink
Hello, http://www.indiahouseraleigh.com/ - diazepam 5mg While some patients will experience some or all of these symptoms, others will not experience anything but a relief of symptoms. http://www.indiahouseraleigh.com/ - valium price
Dypeaccutty
October 16, 2011, October 16, 2011 07:52, permalink
Dypeaccutty, http://www.elongsworld.com/ - discount zyprexa Other off label uses for the medication includes anorexia nervosa, Tourette syndrome, and stuttering. http://www.elongsworld.com/ - zyprexa for sale
spoloNeew
October 20, 2011, October 20, 2011 22:37, permalink
Hello, http://www.tamoxifenforsale.com/ - cheap tamoxifen So what can be done about пїЅmale breastsпїЅ? There are two options currently available; surgery and Nolvadex. http://www.tamoxifenforsale.com/ - purchase nolvadex
ENGILAEDGED
November 9, 2011, November 09, 2011 03:57, permalink
ENGILAEDGED, http://www.tadalafilohnerezept.com/ - cialis kaufen Es wird nicht empfohlen, Cialis zu nehmen, wenn Sie Probleme mit Ihrem Blutdruck haben oder entsprechende Medikamente einnehmen. http://www.tadalafilohnerezept.com/ - cialis online kaufen
Inerencominib
December 4, 2011, December 04, 2011 07:11, permalink
Inerencominib, http://www.microdynamic.org/ - clonazepam no prescription Hyperekplexia, restless legs syndrome, and parasomnia are also successfully treated with Klonopin. http://www.microdynamic.org/ - discount klonopin
canadian pharmacies
January 20, 2012, January 20, 2012 03:44, permalink
pharmacy one source american association of colleges of pharmacy osco pharmacy locator
Since path.exists and fs.stat are asynchronous functions I had to create a tree like this. Any suggestions how I can do it with a for loop?