From 10623f6625f112d709e699860ba018071b5fd202 Mon Sep 17 00:00:00 2001 From: David Diaz Date: Fri, 20 Oct 2017 19:33:35 -0600 Subject: [PATCH] Replaced some promises with async/await. --- request.js | 77 ++++++++++++++++++++++++-------------------------- requestList.js | 42 +++++++++++++-------------- 2 files changed, 58 insertions(+), 61 deletions(-) diff --git a/request.js b/request.js index 20b802c..0f7e0d1 100644 --- a/request.js +++ b/request.js @@ -52,25 +52,27 @@ class Request { return set; } - exec(modifiers = []) { + async exec(modifiers = []) { let dependencies = Array.from(this.DEPENDENCIES); - return this.list.fetchDependencies(dependencies).then(cache => { - let settings = { - endpoint: cache.parse(this.ENDPOINT), - method: this.VERB, - headers: cache.parse(this.HEADERS), - query: cache.parse(this.PARAMS), - payload: cache.parse(this.PAYLOAD) - }; + let cache = await this.list.fetchDependencies(dependencies); - modifiers.forEach(mod => { - if (typeof mod.preRequest !== 'undefined') { - mod.preRequest(settings, this.originalRequest); - } - }); + let settings = { + endpoint: cache.parse(this.ENDPOINT), + method: this.VERB, + headers: cache.parse(this.HEADERS), + query: cache.parse(this.PARAMS), + payload: cache.parse(this.PAYLOAD) + }; - return request({ + modifiers.forEach(mod => { + if (typeof mod.preRequest !== 'undefined') { + mod.preRequest(settings, this.originalRequest); + } + }); + + try { + let response = await request({ url: settings.endpoint, method: settings.method, headers: settings.headers, @@ -80,33 +82,28 @@ class Request { json: true, simple: false, resolveWithFullResponse: true - }) - - .then(response => { - let results = { - request: { - headers: response.request.headers, - body: response.request.body, - endpoint: response.request.uri.href - }, - response: { - status: response.statusCode, - headers: response.headers, - body: response.body - }, - body: response.body - }; - - cache.add(`$${this.ALIAS}`, results); - - return results; - }) - - .catch(function({error}) { - throw new Error(error.message); }); - }); + let results = { + request: { + headers: response.request.headers, + body: response.request.body, + endpoint: response.request.uri.href + }, + response: { + status: response.statusCode, + headers: response.headers, + body: response.body + }, + body: response.body + }; + + cache.add(`$${this.ALIAS}`, results); + + return results; + } catch({error}) { + throw new Error(error); + } } } diff --git a/requestList.js b/requestList.js index 105b407..6c598b1 100644 --- a/requestList.js +++ b/requestList.js @@ -12,42 +12,42 @@ class RequestList { this.cache = new RequestCache(); } - execByAlias(alias) { + async execByAlias(alias) { let request = this.list.find(r => r.ALIAS === alias); if (typeof request === 'undefined') { return Promise.reject(`${alias} not found among the requests.`); } - return request - .exec(this.modifiers) - .then(res => { - this.modifiers.forEach(mod => { - if (typeof mod.postResponse !== 'undefined') { - mod.postResponse(res); - } - }); + try { + let response = await request.exec(this.modifiers); - return res; - }) - .catch(reason => { - return Promise - .reject(`Request: ${request.VERB} ${request.ENDPOINT} FAILED. \n${reason}`); + this.modifiers.forEach(mod => { + if (typeof mod.postResponse !== 'undefined') { + mod.postResponse(response); + } }); + + return response; + } catch (reason) { + throw new Error( + `Request: ${request.VERB} ${request.ENDPOINT} FAILED. \n${reason}` + ); + } } - fetchDependencies(dependencies) { + async fetchDependencies(dependencies) { dependencies = dependencies.map(d => this.execByAlias(d)); + await Promise.all(dependencies); - return Promise.all(dependencies).then(() => this.cache); + return this.cache; } loadRequests(doc) { - let requestKeys = Object.keys(doc) - .filter(key => { - let verb = key.split(' ')[0].toUpperCase(); - return httpVerbs.indexOf(verb) > -1; - }); + let requestKeys = Object.keys(doc).filter(key => { + let verb = key.split(' ')[0].toUpperCase(); + return httpVerbs.indexOf(verb) > -1; + }); return requestKeys.map(key => { doc[key] = doc[key] || {};