Browse Source

chore: improve existing error preservation in promisify (backport: 5-0-x) (#16817)

* chore: improve existing error preservation in promisify

* add a spec
trop[bot] 6 years ago
parent
commit
0f54df5867
2 changed files with 21 additions and 1 deletions
  1. 3 1
      lib/common/api/deprecate.js
  2. 18 0
      spec/api-deprecations-spec.js

+ 3 - 1
lib/common/api/deprecate.js

@@ -99,7 +99,9 @@ const deprecate = {
             cb.length === 2 ? cb(null, res) : cb(res)
           })
         }, err => {
-          process.nextTick(() => cb(err))
+          process.nextTick(() => {
+            cb.length === 2 ? cb(err) : cb()
+          })
         })
     }
   },

+ 18 - 0
spec/api-deprecations-spec.js

@@ -145,6 +145,24 @@ describe('deprecations', () => {
       expect(warnings).to.have.lengthOf(0)
     })
 
+    it('only calls back an error if the callback is called with (err, data)', (done) => {
+      enableCallbackWarnings()
+      let erringPromiseFunc = () => new Promise((resolve, reject) => {
+        reject(new Error('fail'))
+      })
+      erringPromiseFunc = deprecate.promisify(erringPromiseFunc)
+
+      erringPromiseFunc((err, data) => {
+        expect(data).to.be.an('undefined')
+        expect(err).to.be.an.instanceOf(Error).with.property('message', 'fail')
+        erringPromiseFunc(data => {
+          expect(data).to.not.be.an.instanceOf(Error)
+          expect(data).to.be.an('undefined')
+          done()
+        })
+      })
+    })
+
     it('warns exactly once for callback-based invocations', (done) => {
       enableCallbackWarnings()
       promiseFunc = deprecate.promisify(promiseFunc)