From d23c048304e734e6cd9f9ea1bb4d4ce2efb09d19 Mon Sep 17 00:00:00 2001 From: Michael Diarmid Date: Wed, 20 Sep 2017 11:50:34 +0100 Subject: [PATCH] Fix ThenanbleRef promise --- lib/modules/database/reference.js | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/lib/modules/database/reference.js b/lib/modules/database/reference.js index 5f1bc323..9c908585 100644 --- a/lib/modules/database/reference.js +++ b/lib/modules/database/reference.js @@ -481,30 +481,38 @@ export default class Reference extends ReferenceBase { * Access then method of promise if set * @return {*} */ - get then() { - if (this._promise && this._promise.then) { - return this._promise.then.bind(this._promise)(() => { + then(fnResolve, fnReject) { + if (isFunction(fnResolve) && this._promise && this._promise.then) { + return this._promise.then.bind(this._promise)((result) => { this._promise = null; - return this; + return fnResolve(result); + }, (possibleErr) => { + this._promise = null; + + if (isFunction(fnReject)) { + return fnReject(possibleErr); + } + + throw possibleErr; }); } - return undefined; + return throw new Error("Cannot read property 'then' of undefined."); } /** * Access catch method of promise if set * @return {*} */ - get catch() { - if (this._promise && this._promise.catch) { - return this._promise.catch.bind(this._promise)((exception) => { + catch(fnReject) { + if (isFunction(fnReject) && this._promise && this._promise.catch) { + return this._promise.catch.bind(this._promise)((possibleErr) => { this._promise = null; - return Promise.reject(exception); + return fnReject(possibleErr); }); } - return undefined; + return throw new Error("Cannot read property 'catch' of undefined."); } /**