Joe Crane
Joe Crane

Reputation: 57

Promises when passing a function as a parameter

I understand how promises work for the most part, but I have a lot of trouble understanding how to deal with them when I need to pass a function as a parameter:

var promise = new Promise(function(resolve, reject) {
    // Do async job
    ec2.describeInstances(function(err, data) {
        console.log("\nIn describe instances:\n");
        var list = [];
        if (err) reject(err); // an error occurred
        else {
            var i = 0 ;
            //console.log(data.Reservations);
            var reservations = data.Reservations;
            for (var i in reservations) {
                var instances = reservations[i]['Instances'];
                var j = 0;
                //console.log(JSON.stringify(instances, null, 2));
                for (j in instances){
                    var tags = instances[j]
                    var k = 0;
                    var instanceId = tags['InstanceId'];
                    var tag = tags['Tags'];
                    var l;
                    //console.log(tag);

                    for (l in tag){
                        //console.log(instanceId);
                        //console.log(tag[l]['Value']);
                        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);

                            //return(list);
                        }
                    }
                }
            }       
        resolve(list);
        }
    });

});

promise.then(function (list) {
    ec2.terminateInstances(list, function(err, data) {
        if (err) console.log(err, err.stack); // an error occurred
        else     console.log("made it");  });
});

before I had the first part of the code as:

return new Promise(function(resolve, reject) { ... }

and that worked for the first part, but as soon as I changed it to a "var" and added the new promise in underneath, it stopped working. (edit) When I mean "stopped working" I mean, neither of the two functions run, i.e.: it ends the handler before either functions are finished and none of the return statements or console logs.

Any help would be greatly appreciated!

Thanks!

Upvotes: 0

Views: 107

Answers (4)

Joe Crane
Joe Crane

Reputation: 57

I ended up fixing it. Sorry, forgot to post back before I added in the SNS portion. I ended up learning a ton about functional programming on the way and decided to use the await function over the complicated promise syntax. Below is the code:

exports.handler = async (event, result, callback) => {

    const AWS  = require('aws-sdk');
    const date = new Date().toISOString().substr(0, 16)
    const ec2  = new AWS.EC2();
    var sns = new AWS.SNS();

    console.log("date is: " + date)
    console.log(date.length);

    const params = {
            TopicArn:'arn:aws:sns:us-east-1:503782973686:test',
            Message:'Success!!! ',
            Subject: 'TestSNS'
        }

    const describeResult = await ec2.describeInstances().promise()

    const terminatableInstances = await describeResult
        .Reservations
        .reduce((acc, reservation) => acc.concat(reservation.Instances), [])
        //'2018-10-15T23:45'
        .map((instance) => {
            //console.log(instance)
            //console.log(instance.Tags)
            var date = instance.Tags
            .filter(tag => tag.Key == 'killdate' && tag.Value == date) //date should be in this format on tag: 2018-10-15T23:45
            .reduce((acc, curr) => curr.Value, null);
            if (date != null) {
                return instance.InstanceId;
            }
            return null;
        })
        .filter(id  => id != null)


    console.log(terminatableInstances);

    const snsPush = await ec2.terminateInstances({
        InstanceIds: terminatableInstances,
        //DryRun: true //set this flag if you want to do a dry run without terming instances
    }, (err, data) => {

        if (err) {
            console.log('no instances to terminate!')
        }
        else {
            console.log('terminated instances')
        }

    })

    console.log(snsPush)
    //return(snsPush).promise()
    return sns.publish(params, (err, data) => {
        if (err) {
            console.log(err, err.stack); 
        }
        else {
             console.log('sent');
        }
        }).promise(); 


};

Upvotes: 0

apeltz
apeltz

Reputation: 71

var promise = Promise.resolve();

promise
    .then(function() {
        return ec2.describeInstances(function(err, data) {
            var list = [];
            if (err) throw err; // an error occurred
            // else logic
        })
    })
    .catch(/* if needed here */)
    .then(function (list) {
        return ec2.terminateInstances(list, function(err, data) {
            if (err) console.log(err, err.stack); // an error occurred
            else     console.log("made it");  });
    })
    .catch(/* if needed here */)

Upvotes: 1

amir cohen
amir cohen

Reputation: 1

my suggestion is to break up your logic - it will be easier to handle the result you want to achieve.

A proper way in my opinion:

promise function(a service function):

 function myAsyncFunction(url) {
    return new Promise((resolve, reject) => {
        result = () => resolve(data);
        fail = () => reject(err);
    });
}

then your promise caller:

myAsyncFunction().then(dataHandler(result), // "promise worked!"
    function (err) {// Error: "It broke"
        console.log(err)
    });

then the logic:

    function dataHandler(data) { /* data logic */}

good luck

Upvotes: 0

apeltz
apeltz

Reputation: 71

Wondering if something like this would work:

var promise = Promise.resolve(function() {
    return ec2.describeInstances...
})

promise
    .then(/* handle successful promise resolution */ )
    .catch(/* handle promise rejection */ )

Upvotes: 1

Related Questions