DeprecationWarning: Calling an asynchronous functi

2020-02-08 23:50发布

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?

6条回答
我命由我不由天
2楼-- · 2020-02-09 00:25

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

查看更多
别忘想泡老子
3楼-- · 2020-02-09 00:33

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.

查看更多
何必那么认真
4楼-- · 2020-02-09 00:34

I prefer the following two methods, myself.

1:

fs.writeFile('example.md', data, (error) => { console.log("Error!"); });

2:

fs.writeFile('example.md', data, function (err) {
    if(err){
        throw err;
    }
});
查看更多
Deceive 欺骗
5楼-- · 2020-02-09 00:39

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.

查看更多
唯我独甜
6楼-- · 2020-02-09 00:43

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.

查看更多
劫难
7楼-- · 2020-02-09 00:44

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.!');
    }
});
查看更多
登录 后发表回答