From 4b1fee8146fae803cb6afc5eeb24b1d17bcc47d9 Mon Sep 17 00:00:00 2001 From: Lawrence Date: Tue, 24 Sep 2024 08:43:00 +0100 Subject: [PATCH] refactor: async function should have await expression --- src/engine/FileEngine.js | 16 ++++++++-------- src/engine/HTTPEngine.test.js | 30 ++++++++++-------------------- 2 files changed, 18 insertions(+), 28 deletions(-) diff --git a/src/engine/FileEngine.js b/src/engine/FileEngine.js index 0fb0a14..cedb100 100644 --- a/src/engine/FileEngine.js +++ b/src/engine/FileEngine.js @@ -54,10 +54,10 @@ class FileEngine extends Engine { * Retrieves a model by its ID from the file system. * * @param {string} id - The ID of the model to retrieve. - * @returns {Object} The parsed model data. + * @returns {Promise} The parsed model data. * @throws {Error} Throws if the file cannot be read or parsed. */ - static async getById(id) { + static getById(id) { return this.configuration.filesystem .readFile(join(this.configuration.path, `${id}.json`)) .then((b) => b.toString()) @@ -68,10 +68,10 @@ class FileEngine extends Engine { * Retrieves the index for a given model from the file system. * * @param {Model.constructor?} model - The model for which the index is retrieved. - * @returns {Object} The parsed index data. + * @returns {Promise} The parsed index data. * @throws {Error} Throws if the file cannot be read. */ - static async getIndex(model) { + static getIndex(model) { return this.configuration.filesystem .readFile(join(this.configuration.path, model.toString(), '_index.json')) .then((b) => b.toString()) @@ -128,10 +128,10 @@ class FileEngine extends Engine { * Retrieves the compiled search index for a model from the file system. * * @param {Model.constructor} model - The model for which the search index is retrieved. - * @returns {Object} The parsed compiled search index. + * @returns {Promise} The parsed compiled search index. * @throws {Error} Throws if the file cannot be read. */ - static async getSearchIndexCompiled(model) { + static getSearchIndexCompiled(model) { return this.configuration.filesystem .readFile(join(this.configuration.path, model.toString(), '_search_index.json')) .then((b) => b.toString()) @@ -142,10 +142,10 @@ class FileEngine extends Engine { * Retrieves the raw search index for a model from the file system. * * @param {Model.constructor} model - The model for which the raw search index is retrieved. - * @returns {Object} The parsed raw search index. + * @returns {Promise} The parsed raw search index. * @throws {Error} Throws if the file cannot be read. */ - static async getSearchIndexRaw(model) { + static getSearchIndexRaw(model) { return this.configuration.filesystem .readFile(join(this.configuration.path, model.toString(), '_search_index_raw.json')) .then((b) => b.toString()) diff --git a/src/engine/HTTPEngine.test.js b/src/engine/HTTPEngine.test.js index e2336a3..afa4398 100644 --- a/src/engine/HTTPEngine.test.js +++ b/src/engine/HTTPEngine.test.js @@ -267,16 +267,14 @@ test('HTTPEngine.put(model) when the engine fails to put a compiled search index return Promise.resolve({ ok: false, status: 500, - json: async () => { - throw new Error(); - }, + json: () => Promise.reject(new Error()), }); } return Promise.resolve({ ok: true, status: 200, - json: async () => ({}), + json: () => Promise.resolve({}), }); }); @@ -332,16 +330,14 @@ test('HTTPEngine.put(model) when the engine fails to put a raw search index', as return Promise.resolve({ ok: false, status: 500, - json: async () => { - throw new Error(); - }, + json: () => Promise.reject(new Error()), }); } return Promise.resolve({ ok: true, status: 200, - json: async () => ({}), + json: () => Promise.resolve({}), }); }); @@ -388,16 +384,14 @@ test('HTTPEngine.put(model) when putting an index fails', async t => { return Promise.resolve({ ok: false, status: 500, - json: async () => { - throw new Error(); - }, + json: () => Promise.reject(new Error()), }); } return Promise.resolve({ ok: true, status: 200, - json: async () => ({}), + json: () => Promise.resolve({}), }); }); @@ -444,16 +438,14 @@ test('HTTPEngine.put(model) when the initial model put fails', async t => { return Promise.resolve({ ok: false, status: 500, - json: async () => { - throw new Error(); - }, + json: () => Promise.reject(new Error()), }); } return Promise.resolve({ ok: true, status: 200, - json: async () => ({}), + json: () => Promise.resolve({}), }); }); @@ -489,16 +481,14 @@ test('HTTPEngine.put(model) when the engine fails to put a linked model', async return Promise.resolve({ ok: false, status: 500, - json: async () => { - throw new Error(); - }, + json: () => Promise.reject(new Error()), }); } return Promise.resolve({ ok: true, status: 200, - json: async () => ({}), + json: () => Promise.resolve({}), }); });