fs-extra-promise
fs-extra-promise copied to clipboard
writeFile, writeJson resolve before finishing writing
When I try to use these methods they will work, but they call resolve before they actually write their files. So what happens is that the next chain expects the file to be there, and work on it, except I get a ENOENT.
The only way I cant get this to work is to go back to default fs.
var Promise = require("bluebird")
var fs = require("fs");
//works
return new Promise(function(resolve, reject){
fs.writeFile(currentJsonPath, "{}", function(e, data){
if (e){
reject(e)
} else {
resolve()
}
});
});
//writes file eventually, but calls resolve early
var fsExtra = Promise.promisifyAll(require("fs-extra"));
return fsExtra.writeFile(currentJsonPath, "{}");
//writes file eventually, but calls resolve early
var fsExtraPromise= require("fs-extra-promise");
return fsExtraPromise.writeFile(currentJsonPath, "{}");
Oh shit! This is a really bad bug.
But I suspect this is a bug in fs-extra rather than fs-extra-promise.
Just to make sure this has nothing to do with promisification, could you possibly try this:
var fsExtra = require('fs-extra');
fsExtra.writeFile( currentJsonPath, "{}", function(e) {
if (e) throw e;
// check file has been written
} );
Yeah, I think I agree it's probably fs-extra.
I'm using https://www.npmjs.com/package/json-file-pluspackage, and the file is still not there when I write it as a callback
var jsonFile = require("json-file-plus")
var fsExtra = require('fs-extra');
fsExtra.writeFile( currentJsonPath, "{}", function(e) {
if (e) throw e; //is null
jsonFile(currentJsonPath) //but file is not actually there
});
fsExtra.writeFile( currentJsonPath, "{}", function(e) {
if (e) throw e;
setTimeout(jsonFile(currentJsonPath), 500) //no error, and works as expected
});
Just to remove the possibility of json-file-plus being the location of the fault, could you please try using a vanilla fs method like fs.readFile to check the file's existence?
But yes, if that still fails, it's definitely a bug in fs-extra. And a pretty bad one if you ask me.
@1mike12 How you getting on with this? Would be good to narrow this down so can raise an issue on fs-extra if that's where the fault is.
By the way, what OS and version of Node are you using?
Ping @1mike12!
Ping @1mike12! Did you ever work out what the problem was here? If it's a bug in fs-extra-promise, I'd like to fix it, but it did look like the culprit was more likely to be in fs-extra. Please let me know so I can close this issue if it's solved.
yeah I've been super busy, I was trying to write a quick test this morning with a forked vers of this, but ran into some trouble. I'll try to get it up b4 this weekend ends
@1mike12 Any progress with this? NB Have just released v1.0.0 which has latest versions of fs-extra and bluebird. So they may have solved this problem in the meantime.