0

I've got an AWS call that looks like this:

ecs.describeServices(params, function(err, data) {
   if (err) {
      return { data: '',success: false };
   } else {
      return { data: 'whohoo',success: true};
   }
});

This is in a method that needs to return a promise that resolves to my return object.

I've been experimenting with this type solution, but just can't figure it out.

        var keepsHisWord;
        keepsHisWord = true;
        var promise1 = new Promise(function(resolve, reject) {
            if (keepsHisWord) {
                resolve({ mytest: "mytest101" });
            } else {
                reject("The man doesnt want to keep his word");
            }
        });

Edit: Working. Thanks to the two people who posted. That really helped a lot. And wondering why the question down-voted. I did a bunch of research first. I guess just not as smart at the down-voter.

            var promise1 = function(params) {
            return new Promise(function(resolve, reject) {
                ecs.describeServices(params, function(err, data) {
                    if (err) {
                        resolve({
                            error: "my error"
                        });
                    } else {
                        resolve({
                            runningCount: data.services[0].runningCount,
                            pendingCount: data.services[0].pendingCount,
                            status: data.services[0].status,
                            desiredCount: data.services[0].desiredCount,
                            createdAt: data.services[0].desiredCount,
                            events: data.services[0].events
                        });
                    }
                });
            });
        };
        return promise1(params);
Pete
  • 3,111
  • 6
  • 20
  • 43

2 Answers2

1

Wrap your service call into promise:

promiseFunc = function(params) {


    return new Promise(function(resolve,reject){

        ecs.describeServices(params, function(err, data) {
            if (err) {
                resolve({ data: '',success: false });
            } else {
                reject({ data: 'whohoo',success: true});
            }
        });

    })

}

Then use it:

 promiseFunc(params).then(x => {})
enno.void
  • 6,242
  • 4
  • 25
  • 42
  • I can get your example to work, my success method does get called (I can put console.log to see that). In my api, I need to return the promise so I do "return promiseFunc(params);", but then my api seems to not know how to get the answer. does the caller simply execute my .then function? If so, I tried promiseFunc(params).then(x => {return x.data;}) and got error still. – Pete Oct 08 '18 at 18:44
0

Starting from Node 8 there is a util.promisify function, which does exactly that

var util = require('util');
var describeServices = util.promisify(ecs.describeServices);
describeServices(params)
    .then()
    .catch()

You also can find many implementations of this function in the internet.

Sergii Vorobei
  • 1,477
  • 13
  • 19
  • Thanks @Sergii, I don't think I quite get it. I added code I tried in the question above but without success. – Pete Oct 08 '18 at 18:17
  • You should return promise1 directly: e.g. `return promise1.then().catch()` as `promise1` and `promise1.then()` are different promises – Sergii Vorobei Oct 08 '18 at 18:19
  • Thanks @Sergii but I'm still not getting it. I tried what I think you suggested (updated in question above) and my error condition hits (odd) and I get the error "TypeError: promise1.then is not a function" – Pete Oct 08 '18 at 18:32
  • Sorry, I meant `return promise1(params) .then((data) => { console.log(data.services[0].runningCount); return { runningCount: data.services[0].runningCount }; }) .catch((error) => { console.log("error:" + JSON.stringify(error)); return { runningCount: -1 }; });` Does the error still appear? – Sergii Vorobei Oct 08 '18 at 18:43
  • that runs, and returns my error condition. somehow, the parameters are not getting passed into ecs.describeServices. – Pete Oct 08 '18 at 18:49
  • I pasted above what I have. – Pete Oct 08 '18 at 18:51
  • Got it! thanks for the help. I posted my final above. – Pete Oct 08 '18 at 19:01