So I have forked a Javascript project and trying to extend it a bit and at the same time learn from it. My Javascript skills are really newbish but I thought it would be fun. I am however really struggling with all the promises to the point that I have some many promises and thenables that I really don't understand how it is situated anymore. I fail to get the end result and are unable to see why.
So I have this to start from (I disabled one function to keep it simple):
export const calculateMovingAverage = (event, context, callback) =>
Promise.all([
// ma.calculateMovingAverage('Kraken', 'AskPrice'),
ma.calculateMovingAverage('Coinbase', 'SellPrice'),
])
.then((tx) => {
console.log('tx', tx);
}).catch((err) => {
console.error('err', err);
callback({ statusCode: 500, body: { message: 'Something went wrong' } });
});
Which thus calls ma.calculateMovingAverage()
:
calculateMovingAverage(exchange, metric) {
const self = this;
const args = {
minutes: 10,
period: 60,
currency: `${self.cryptoCurrency}`,
metricname: `${metric}`,
localCurrency: `${self.localCurrency}`,
namespace: `${exchange}`,
};
var promisedland = new Promise((resolve, reject) => {
self.cloudwatch.getMetricStatistics(args, (err, data) => {
if (err) { return reject(err); }
if (!Array.isArray(data.Datapoints) || !data.Datapoints.length) { return reject("No Datapoints received from CloudWatch!")}
data.Datapoints.forEach(function(item) {
self.ma.push(item.Timestamp, item.Average);
});
resolve(ma.movingAverage());
})
})
promisedland.then((results) => {
return new Promise((resolve, reject) => {
const body = {
value: results,
metricName: `${metric} @ 180 MovingAverage`,
namespace: `${exchange}`
};
self.cloudwatch.putAverageMetricData(body, function(err, result) {
if (err) { return reject(err); }
resolve(result);
});
}
)
}).catch(function(err) {
return reject(err);
});
}
Now as you can see inside calculateMovingAverage() I try to call two AWS methods. getMetricStatistics
and putAverageMetricData
.
The first one, the getMetricStatistics functions works beautifully as I get back the Datapoints nicely from AWS.
The function itself:
getMetricStatistics(args) {
return this.cloudwatch.getMetricStatistics({
EndTime: moment().subtract(180, 'minutes').utc().format(),
Dimensions: [
{
Name: 'CryptoCurrency',
Value: args.currency
},
{
Name: 'LocalCurrency',
Value: args.localCurrency
},
{
Name: 'Stage',
Value: process.env.STAGE || 'dev'
}
],
MetricName: args.metricname,
Period: Number(args.period),
StartTime: moment().subtract(190, 'minutes').utc().format(),
Statistics: ['Average'],
Unit: 'Count',
Namespace: args.namespace || 'Coinboss',
}).promise();
}
Next I want to pass on the response through the MovingAverage module and would like to put the outcome of MA into CloudWatch Metrics via the putAverageMetricData function:
putAverageMetricData(args) {
return this.cloudwatch.putMetricData({
MetricData: [
{
MetricName: args.metricName,
Timestamp: moment().utc().format(),
Unit: 'Count',
Value: Number(args.value),
},
],
Namespace: args.namespace || 'Coinboss',
}).promise()
.then(function(result) {
console.log('putAverageMetricData', result);
});
}
This is where I get lost. I looks like the data never arrives at the putAverageMetricData function. The console output only shows me the console.log('tx', tx);
with the following:
2017-07-15T19:37:43.670Z 118ff4f0-6995-11e7-8ae7-dd68094efbd6 tx [ undefined ]
Ok, so I was not returning the calculateMovingAverage() then
. It solved the undefined error. I still don't get logging from the putAverageMetricData() function which leaves me to think I am still missing something.
I hope someone can point me into the right direction