clarify use of promises

This commit is contained in:
AJ ONeal 2015-12-14 23:04:55 -08:00
parent 4d030c6216
commit cbba2bbbf0
2 changed files with 36 additions and 27 deletions

View File

@ -38,30 +38,19 @@ In Node
I ported that proccess to node.
```
sfs.writeFileAsync
sfs.stageAsync
sfs.commitAsync
sfs.revertAsync
```
```js
// default behavior is to concat (filename + '.' + 'new')
var safeReplace = require('safe-replace').create({ new: 'new', bak: 'bak' });
// default behavior is to concat (filename + '.' + rnd() + '.tmp')
var safeReplace = require('safe-replace').create({ tmp: 'tmp', bak: 'bak' });
var data = new Buffer('A priceless document');
safeReplace.writeFile('keep.txt', data, 'ascii').then(function () {
fs.readdir('.', function (nodes) {
console.log('file system nodes', nodes);
// keep.txt
// keep.txt.bak
});
});
// let's say I wrote keep.txt.x7t7sq926.tmp with my own mechanism
safeReplace.commit('keep.txt.x7t7sq926.tmp', 'keep.txt').then(function () {
fs.readdir('.', function (nodes) {
console.log('file system nodes', nodes);
// keep.txt
// keep.txt.bak
});
});
// let's say I want to revert the file from the '.bak'
safeReplace.revert('keep.txt').then(function () {
safeReplace.writeFileAsync('keep.txt', data, 'ascii').then(function () {
fs.readdir('.', function (nodes) {
console.log('file system nodes', nodes);
// keep.txt
@ -70,12 +59,28 @@ safeReplace.revert('keep.txt').then(function () {
});
// let's say I want to write a tmp file and not commit it... weird
safeReplace.stage('keep.txt', data, 'ascii').then(function (tmpname) {
safeReplace.stageAsync('keep.txt', data, 'ascii').then(function (tmpname) {
fs.readdir('.', function (nodes) {
console.log('file system nodes', nodes);
// keep.txt.ac71teh8mja.tmp
});
});
// let's say I wrote keep.txt.x7t7sq926.tmp with my own mechanism
safeReplace.commitAsync('keep.txt.x7t7sq926.tmp', 'keep.txt').then(function () {
fs.readdir('.', function (nodes) {
console.log('file system nodes', nodes);
// keep.txt
// keep.txt.bak
});
});
// let's say I want to revert the file from the '.bak'
safeReplace.revertAsync('keep.txt').then(function () {
fs.readdir('.', function (nodes) {
console.log('file system nodes', nodes);
// keep.txt
// keep.txt.bak
// keep.txt.ac71teh8mja.tmp
});
});
```

View File

@ -35,20 +35,20 @@ function create(options) {
*/
var sfs = {
writeFile: function (filename, data, options) {
writeFileAsync: function (filename, data, options) {
return sfs.stage(filename, data, options).then(function (tmpname) {
//console.log(filename);
return sfs.commit(tmpname, filename);
});
}
, stage: function (filename, data, options) {
, stageAsync: function (filename, data, options) {
var tmpname = tmpnamefn(filename);
//console.log(tmpname);
return fs.writeFileAsync(tmpname, data, options).then(function () {
return tmpname;
});
}
, commit: function (tmpname, filename) {
, commitAsync: function (tmpname, filename) {
var bakname = baknamefn(filename);
// this may not exist
return fs.unlinkAsync(bakname).then(noop, noop).then(function () {
@ -68,7 +68,7 @@ function create(options) {
});
});
}
, revert: function (filename) {
, revertAsync: function (filename) {
return new PromiseA(function (resolve, reject) {
var bakname = baknamefn(filename);
var tmpname = tmpnamefn(filename);
@ -89,6 +89,10 @@ function create(options) {
, baknamefn: baknamefn
, create: create
};
sfs.writeFile = sfs.writeFileAsync;
sfs.stage = sfs.stageAsync;
sfs.commit = sfs.commitAsync;
sfs.revert = sfs.revertAsync;
return sfs;
}