From 8bb7185c7aa96bc626f664272cf046889a2ba90a Mon Sep 17 00:00:00 2001 From: sachaaaaa <sacha@loki.network> Date: Thu, 23 Aug 2018 15:04:59 +1000 Subject: [PATCH] make loki_message_api::sendMessage use async/await --- js/modules/loki_message_api.js | 98 ++++++++++++++++------------------ 1 file changed, 47 insertions(+), 51 deletions(-) diff --git a/js/modules/loki_message_api.js b/js/modules/loki_message_api.js index e769076c1..375232d47 100644 --- a/js/modules/loki_message_api.js +++ b/js/modules/loki_message_api.js @@ -19,7 +19,7 @@ function initialize({ url }) { sendMessage }; - function sendMessage(pub_key, data, ttl) + async function sendMessage(pub_key, data, ttl) { const options = { url: `${url}/send_message`, @@ -27,60 +27,56 @@ function initialize({ url }) { responseType: undefined, timeout: undefined }; - - return new Promise((resolve, reject) => { - - log.info(options.type, options.url); - const body = JSON.stringify({ - pub_key, - message: data, - ttl, - }); + log.info(options.type, options.url); + + const body = JSON.stringify({ + pub_key, + message: data, + ttl, + }); - const fetchOptions = { - method: options.type, - body, - headers: { 'X-Loki-Messenger-Agent': 'OWD' }, - timeout: options.timeout, - }; + const fetchOptions = { + method: options.type, + body, + headers: { 'X-Loki-Messenger-Agent': 'OWD' }, + timeout: options.timeout, + }; - fetchOptions.headers['Content-Type'] = 'application/json; charset=utf-8'; + fetchOptions.headers['Content-Type'] = 'application/json; charset=utf-8'; - fetch(options.url, fetchOptions) - .then(response => { - let resultPromise; - if ( - options.responseType === 'json' && - response.headers.get('Content-Type') === 'application/json' - ) { - resultPromise = response.json(); - } else if (options.responseType === 'arraybuffer') { - resultPromise = response.buffer(); - } else { - resultPromise = response.text(); - } - return resultPromise.then(result => { - if (response.status >= 0 && response.status < 400) { - log.info(options.type, options.url, response.status, 'Success'); - resolve(result, response.status); - } else { - log.error(options.type, options.url, response.status, 'Error'); - reject( - HTTPError( - 'promiseAjax: error response', - response.status, - result - ) - ); - } - }); - }) - .catch(e => { - log.error(options.type, options.url, 0, 'Error'); - reject(HTTPError('promiseAjax catch', 0, e.toString())); - }); - }); + let response; + try { + response = await fetch(options.url, fetchOptions); + } + catch(e) { + log.error(options.type, options.url, 0, 'Error'); + throw HTTPError('fetch error', 0, e.toString()); + } + + let result; + if ( + options.responseType === 'json' && + response.headers.get('Content-Type') === 'application/json' + ) { + result = await response.json(); + } else if (options.responseType === 'arraybuffer') { + result = await response.buffer(); + } else { + result = await response.text(); + } + + if (response.status >= 0 && response.status < 400) { + log.info(options.type, options.url, response.status, 'Success'); + return [result, response.status]; + } else { + log.error(options.type, options.url, response.status, 'Error'); + throw HTTPError( + 'sendMessage: error response', + response.status, + result + ); + } } } }