From 3e833309dbefc4e40833e5fb9f7550dad1569f18 Mon Sep 17 00:00:00 2001 From: 18alantom <2.alan.tom@gmail.com> Date: Mon, 8 Nov 2021 19:35:37 +0530 Subject: [PATCH] fix: prevent error slipping because of asyncness --- model/document.js | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/model/document.js b/model/document.js index fec4445c..1c79e925 100644 --- a/model/document.js +++ b/model/document.js @@ -608,11 +608,11 @@ module.exports = class BaseDocument extends Observable { return this; } - insertOrUpdate() { + async insertOrUpdate() { if (this._notInserted) { - return this.insert(); + return await this.insert(); } else { - return this.update(); + return await this.update(); } } @@ -622,14 +622,23 @@ module.exports = class BaseDocument extends Observable { await this.trigger('afterDelete'); } + async submitOrRevert(isSubmit) { + const wasSubmitted = this.submitted; + this.submitted = isSubmit; + try { + await this.update(); + } catch (e) { + this.submitted = wasSubmitted; + throw e; + } + } + async submit() { - this.submitted = 1; - this.update(); + await this.submitOrRevert(1); } async revert() { - this.submitted = 0; - this.update(); + await this.submitOrRevert(0); } async rename(newName) {