Tuesday, 2 April 2019

requestretry with promise?

hi guys,I'm new to node and still trying to get my head around this promise pattern.I'm implementing a function to grab data from an API end point. It works fine so I'm trying to implement requestretryhttps://www.npmjs.com/package/requestretry but for some reason I keep getting the error below (when I test it by stopping apache)"{"errorMessage": "connect ECONNREFUSED xxxxxx:80","errorType": "Error","stackTrace": ["Error: connect ECONNREFUSED xxxxxxx:80"," at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1113:14)"]}"​I'm wondering if I didn't implement request/retry properly? I'm hoping to get some tips from you guys.Thanks​The main function is:class MyModule { get() { data=....... return this._send(data); } ......... ......... ......... _send (data) { return new Promise((resolve, reject) => { request({ uri: 'domain.com', body: JSON.stringify(data), retryDelay: 2000, // (default) wait for 5s before trying again retryStrategy: request.RetryStrategies.HTTPOrNetworkError, // (default) retry on 5xx or network errors fullResponse: true, // (default) To resolve the promise with the full response or just the body } ,(err, response, body) => { if(err) reject(err); let returnValue = body; try { returnValue = JSON.parse(returnValue); } catch(e) { return reject(e); } //success if the code is a 200 type if(response.statusCode >= 200 && response.statusCode < 300) { return resolve(returnValue); } //otherwise fail it :) if(returnValue.parameters) { reject(this._format(returnValue.message, returnValue.parameters, '%{key}')); } else { reject(returnValue.message); } }) }) } } And i'm calling it through an async call like this​ module.exports.orders = async (event, context) => { try { var orders = await MyModule.get(); return { statusCode: 200, body: { message: orders } } } catch (err) { throw err; } } ​

Submitted April 02, 2019 at 12:35PM by duyth

No comments:

Post a Comment