Bluebird: fs.writeFile doesn't return a promise when not promisified explicitly

Created on 23 Dec 2014  路  5Comments  路  Source: petkaantonov/bluebird

when I have code:

var Promise = require('bluebird');

var fs = Promise.promisifyAll(require('fs'));

fs.writeFile('./justATest.txt', 'hello', {}).then(function() {
    console.log("succesfulle written");
});

I get a

fs.writeFile('./justATest.txt', 'hello', {}).then(function() {
                                         ^
TypeError: Cannot call method 'then' of undefined    

It works when I promisify it explicitly:

var Promise = require('bluebird');
var writeFile = Promise.promisify(require('fs').writeFile);

writeFile('./justATest.txt', 'hello', {}).then(function() {
    console.log("succesfulle written");
});

So shouldn't it work even in the first case?

Most helpful comment

@dzuluaga try

fs.writeFileAsync('./justATest.txt', 'hello', {}).then(function() {
    console.log("succesfully written");
});

after promisifying all. That should work according to the docs.

All 5 comments

It doesn't overwrite the methods, since that would break any code using the module. The promise-returning versions have Async suffix by default. More at documentation

@petkaantonov I thought it created a whole new 'proxy' object under the hood. Allright thanks.

I know this has been locked. But I only was able to make it work as per @capaj workaround. I didn't get your comment @petkaantonov. Could you please post the code to support promisifyAll?

@dzuluaga try

fs.writeFileAsync('./justATest.txt', 'hello', {}).then(function() {
    console.log("succesfully written");
});

after promisifying all. That should work according to the docs.

Thanks @capaj. I was just posting the same finding. Thanks again!

Was this page helpful?
0 / 5 - 0 ratings