Reputation: 7734
I recently updated my node to 7.2.1 and noticed that there is a warning coming:
(node:4346) DeprecationWarning: Calling an asynchronous function without callback is deprecated.
What is this 4346
for? I only have 2000 lines in the js file, so it can't be a line-number. Where can I find the code?
Upvotes: 32
Views: 30235
Reputation: 44
just mention:
fs.writeFile('<your file name>',<your data>,function(){});
here, you need to mention function(){} as this is a callback() to write text in asynchronous manner.
Using writeFileSync will make an synchronous call
Upvotes: 1
Reputation: 2273
I got the same warning
[DEP0013] DeprecationWarning: Calling an asynchronous function without callback is deprecated.
and had the same problem of not knowing which part of my code was causing it. So I looked at code I had recently modified and saw this statement-suspect:
Fs . writeFile (path, aString, cb, encoding);
Problem was the cb (= 'callback') and the encoding arguments are in wrong order. I got rid of the warning simply by changing the above to:
Fs . writeFile (path, aString, encoding, cb);
But the problem really is with the ERRONEOUS warning-message. I WAS passing in a callback-argument but just one which was not a Function but a String. So if the warning had said
"WWARNING: calling fs.writeFile() with a string-argument
where a function is expected"
... it would have been obvious what was happening. Of course a line-number in the warning would be nice as well.
So the point is I was NOT calling writeFile() without a callback-argument, which is deprecated. I was calling writeFile() WITH A WRONG TYPE OF ARGUMENT. That should be an ERROR, not a warning.
Upvotes: 0
Reputation: 181
this is because you have not catch error by using err callback use like below in your code
fs.write('./abc.txt',function(err){
if(err){
return console.log(err);
}
else
{
console.log('success.!');
}
});
Upvotes: 1
Reputation: 53
I prefer the following two methods, myself.
fs.writeFile('example.md', data, (error) => { console.log("Error!"); });
fs.writeFile('example.md', data, function (err) {
if(err){
throw err;
}
});
Upvotes: 2
Reputation: 61
You need to include a callback function for the Asynchronous method (writeFile
in your case).
For example
var fs = require('fs');
fs.writeFile('writeMe.txt',data,'utf8',(error)=>{
// your code goes here
});
where
(error) => { });
is the callback function.
From Version: v7.0.0
The callback parameter is no longer optional. Not passing it will emit a deprecation warning.
Please refer: https://nodejs.org/api/fs.html#fs_fs_writefile_file_data_options_callback for more info.
Upvotes: 6
Reputation: 58430
You can use either the --trace-deprecation
or --throw-deprecation
options.
For example:
node --trace-deprecation app.js
or:
node --throw-deprecation app.js
The first option will log a stack trace and the second will throw an error (which, if not caught, will also log a stack trace).
Also, 4346
is most likely the process ID.
Upvotes: 56