While I have a good understanding of how promises function, I often struggle when it comes to passing a function as a parameter:
var promise = new Promise(function(resolve, reject) {
// Perform asynchronous task
ec2.describeInstances(function(err, data) {
console.log("\nIn describe instances:\n");
var list = [];
if (err) reject(err); // handle error
else {
var i = 0 ;
var reservations = data.Reservations;
for (var i in reservations) {
var instances = reservations[i]['Instances'];
var j = 0;
for (j in instances){
var tags = instances[j]
var k = 0;
var instanceId = tags['InstanceId'];
var tag = tags['Tags'];
var l;
for (l in tag){
if (String(tag[l]['Value']) == '2018-10-15T23:45' || String(tag[l]['Key']) == 'killdate') {
console.log(tag[l]['Key'] + ' ' + tag[l]['Value']);
list.push(instanceId);
console.log(list);
}
}
}
}
resolve(list);
}
});
});
promise.then(function (list) {
ec2.terminateInstances(list, function(err, data) {
if (err) console.log(err, err.stack); // handle error
else console.log("made it");
});
});
Prior to this code snippet, my initial setup was:
return new Promise(function(resolve, reject) { ... }
which worked initially. However, after making some changes by declaring it as a "var" and adding a new promise below, the functionality ceased. Specifically, neither of the two functions executed, prematurely exiting the handler without running any return statements or console logs.
I would greatly appreciate any assistance!
Thank you!