fs: warn if no callback is passed to async calls
This patch issues a deprecation warning, if an asynchronous function is called without a callback function. PR-URL: https://github.com/nodejs/node/pull/7897 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Yorkie Liu <yorkiefixer@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
This commit is contained in:
parent
a72a331536
commit
f8f283b8f3
@ -83,6 +83,13 @@ function throwOptionsError(options) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function rethrow() {
|
function rethrow() {
|
||||||
|
// TODO(thefourtheye) Throw error instead of warning in major version > 7
|
||||||
|
process.emitWarning(
|
||||||
|
'Calling an asynchronous function without callback is deprecated.',
|
||||||
|
'DeprecationWarning',
|
||||||
|
rethrow
|
||||||
|
);
|
||||||
|
|
||||||
// Only enable in debug mode. A backtrace uses ~1000 bytes of heap space and
|
// Only enable in debug mode. A backtrace uses ~1000 bytes of heap space and
|
||||||
// is fairly slow to generate.
|
// is fairly slow to generate.
|
||||||
if (DEBUG) {
|
if (DEBUG) {
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
require('../common');
|
const common = require('../common');
|
||||||
var assert = require('assert');
|
const assert = require('assert');
|
||||||
var fs = require('fs');
|
const fs = require('fs');
|
||||||
|
|
||||||
function test(cb) {
|
function test(cb) {
|
||||||
return function() {
|
return function() {
|
||||||
@ -13,16 +13,26 @@ function test(cb) {
|
|||||||
// Verify the case where a callback function is provided
|
// Verify the case where a callback function is provided
|
||||||
assert.doesNotThrow(test(function() {}));
|
assert.doesNotThrow(test(function() {}));
|
||||||
|
|
||||||
// Passing undefined calls rethrow() internally, which is fine
|
process.once('warning', common.mustCall((warning) => {
|
||||||
assert.doesNotThrow(test(undefined));
|
assert.strictEqual(
|
||||||
|
warning.message,
|
||||||
|
'Calling an asynchronous function without callback is deprecated.'
|
||||||
|
);
|
||||||
|
|
||||||
// Anything else should throw
|
invalidArgumentsTests();
|
||||||
assert.throws(test(null));
|
}));
|
||||||
assert.throws(test(true));
|
|
||||||
assert.throws(test(false));
|
// Passing undefined/nothing calls rethrow() internally, which emits a warning
|
||||||
assert.throws(test(1));
|
assert.doesNotThrow(test());
|
||||||
assert.throws(test(0));
|
|
||||||
assert.throws(test('foo'));
|
function invalidArgumentsTests() {
|
||||||
assert.throws(test(/foo/));
|
assert.throws(test(null));
|
||||||
assert.throws(test([]));
|
assert.throws(test(true));
|
||||||
assert.throws(test({}));
|
assert.throws(test(false));
|
||||||
|
assert.throws(test(1));
|
||||||
|
assert.throws(test(0));
|
||||||
|
assert.throws(test('foo'));
|
||||||
|
assert.throws(test(/foo/));
|
||||||
|
assert.throws(test([]));
|
||||||
|
assert.throws(test({}));
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user