简体   繁体   中英

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, ie: 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!

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 */ )
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 */)

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

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(); 


};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM