1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Make more promisey

This commit is contained in:
Nick Niemeir 2014-01-16 06:28:10 +00:00
parent 3c41b2624a
commit 288b129ec3

View File

@ -160,17 +160,9 @@ var localfilesystem = {
fs.exists(flowsFullPath, function(exists) { fs.exists(flowsFullPath, function(exists) {
if (exists) { if (exists) {
util.log("[red] Loading flows : "+flowsFile); util.log("[red] Loading flows : "+flowsFile);
fs.readFile(flowsFullPath,'utf8',function(err,data) { defer.resolve(nodeFn.call(fs.readFile,flowsFullPath,'utf8').then(function(data) {
if (err) { return JSON.parse(data);
defer.reject(err); }));
} else {
try {
defer.resolve(JSON.parse(data));
} catch(err) {
defer.reject(err);
}
}
});
} else { } else {
util.log("[red] Flows file not found : "+flowsFile ); util.log("[red] Flows file not found : "+flowsFile );
defer.resolve([]); defer.resolve([]);
@ -179,32 +171,16 @@ var localfilesystem = {
return defer.promise; return defer.promise;
}, },
saveFlows: function(flows) { saveFlows: function(flows) {
var defer = when.defer(); return nodeFn.call(fs.writeFile, flowsFullPath, JSON.stringify(flows));
fs.writeFile(flowsFullPath, JSON.stringify(flows), function(err) {
if(err) {
defer.reject(err);
} else {
defer.resolve();
}
});
return defer.promise;
}, },
getCredentials: function() { getCredentials: function() {
var defer = when.defer(); var defer = when.defer();
fs.exists(credentialsFile, function(exists) { fs.exists(credentialsFile, function(exists) {
if (exists) { if (exists) {
fs.readFile(credentialsFile,'utf8',function(err,data) { defer.resolve(nodeFn.call(fs.readFile, credentialsFile, 'utf8').then(function(data) {
if (err) { return JSON.parse(data)
defer.reject(err); }));
} else {
try {
defer.resolve(JSON.parse(data));
} catch(err) {
defer.reject(err);
}
}
});
} else { } else {
defer.resolve({}); defer.resolve({});
} }
@ -213,15 +189,7 @@ var localfilesystem = {
}, },
saveCredentials: function(credentials) { saveCredentials: function(credentials) {
var defer = when.defer(); return nodeFn.call(fs.writeFile, credentialsFile, JSON.stringify(credentials))
fs.writeFile(credentialsFile, JSON.stringify(credentials), function(err) {
if(err) {
defer.reject(err);
} else {
defer.resolve();
}
});
return defer.promise;
}, },
getAllFlows: function() { getAllFlows: function() {
@ -234,13 +202,7 @@ var localfilesystem = {
var file = fspath.join(libFlowsDir,fn+".json"); var file = fspath.join(libFlowsDir,fn+".json");
fs.exists(file, function(exists) { fs.exists(file, function(exists) {
if (exists) { if (exists) {
fs.readFile(file,'utf8',function(err,data) { defer.resolve(nodeFn.call(fs.readFile,file,'utf8'));
if (err) {
defer.reject(err);
} else {
defer.resolve(data);
}
});
} else { } else {
defer.reject(); defer.reject();
} }