diff --git a/lib/fs.js b/lib/fs.js index ebe29c578d37d0..7056232e638c63 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -34,6 +34,7 @@ const { ObjectDefineProperties, ObjectDefineProperty, Promise, + PromisePrototypeThen, PromiseResolve, ReflectApply, SafeMap, @@ -3171,15 +3172,11 @@ function glob(pattern, options, callback) { callback = makeCallback(callback); const Glob = lazyGlob(); - // TODO: Use iterator helpers when available - (async () => { - try { - const res = await ArrayFromAsync(new Glob(pattern, options).glob()); - callback(null, res); - } catch (err) { - callback(err); - } - })(); + PromisePrototypeThen( + ArrayFromAsync(new Glob(pattern, options).glob()), + (res) => callback(null, res), + callback, + ); } function globSync(pattern, options) { diff --git a/test/parallel/test-fs-glob-throw.mjs b/test/parallel/test-fs-glob-throw.mjs new file mode 100644 index 00000000000000..df501888b85a82 --- /dev/null +++ b/test/parallel/test-fs-glob-throw.mjs @@ -0,0 +1,16 @@ +import { mustCall } from '../common/index.mjs'; +import { glob } from 'node:fs'; +import process from 'node:process'; +import { strictEqual } from 'node:assert'; + +// One uncaught error is expected +process.on('uncaughtException', mustCall((err) => { + strictEqual(err.message, 'blep'); +})); + +{ + // Test that if callback throws, it's not getting called again + glob('a/b/c', mustCall(() => { + throw new Error('blep'); + })); +}