question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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?

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
capajcommented, Aug 31, 2015

@dzuluaga try

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

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

0reactions
dzuluagacommented, Aug 31, 2015

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

fs.writeFile in a promise, asynchronous-synchronous stuff
After 6 years, 10 answers and hundreds of votes, still no one has noticed that the for-loop ALWAYS exits in the first iteration...
Read more >
From callbacks to fs/promises to handle the file system in ...
Let's promisify manually and wrap in a function the above code: const fs ... data) => { return new Promise((resolve, reject) => {...
Read more >
An intriguing reason why Node.js libraries aren't promises by ...
So this one's for you, Ravish! Let's deal with the first question first: “Why doesn't the AWS node sdk just return a promise...
Read more >
File system | Node.js v19.3.0 Documentation
The fs/promises API provides asynchronous file system methods that return promises. The promise APIs use the underlying Node.js threadpool to perform file ...
Read more >
Using promises - JavaScript - MDN Web Docs
A Promise is an object representing the eventual completion or failure of an asynchronous operation. Since most people are consumers of ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found