Kolong Kinghu
Kolong Kinghu

Reputation: 41

Trying to resolve a promise chain from ES6 class functions

So I have this class called Events and I'm trying to call an async function from my server. I'm trying to understand where I went wrong / how to resolve a promise inside another async call. Any hints or tips would be greatly appreciated.

This is the event function I'm calling:

Event.prototype.getCost = function(name_of_place){
 return new Promise( function(){
      var placeID;
      var GooglePlaces = require("node-googleplaces");
      const places = new GooglePlaces(API_KEY);
      const params = {
           location: '40.689247,-123.102192',
           radius: 1000
      };
      var query =
      {
            query: name_of_place
       };
       // ASYNC call
       places.textSearch(query).then((res) => {
            console.log("FIRST ASYNC CALL");
            console.log(res.body.results[0].place_id);
            placeID = res.body.results[0].place_id;
            var request_place_details={
                 placeid : placeID
            };
            console.log(request_place_details);

            return request_place_details;

       }).then((request_place_details) => {
            console.log("SECOND ASYNC CALL");
            places.details(request_place_details).then((res) => {
                 console.log(res.body.result.price_level + "   S");
                 var cost = res.body.result.price_level;
                 //trying to resolve getCost promise
                 //resolve(this.cost);
                 return cost;
            }).then((cost) => {
                 console.log("ATTEMPT TO RESOLVE ORIGINAL");
                 this.cost = cost;
                 console.log(cost + "   F");

                 //WANT TO RETURN THIS VALUE IN THE END
                 resolve(this.cost);
            });
       });
  })};

This is where I'm calling it from:

//Server is currently serving on port 8420
app.listen(8420,function startServer(){
 console.log("Listening on :: " + 8420);
 var Event1 = new events(7,10,'New York');
 // console.log(Event1.getCost('Sushi'));
 Event1.getCost('Sushi').then(res => {
      console.log(res);
 })
});

Upvotes: 0

Views: 88

Answers (2)

Thomas
Thomas

Reputation: 12637

your whole structure is way to complicated. when stripping the comments, you can reduce the code to this:

Event.prototype.getCost = function(name_of_place){
    var GooglePlaces = require("node-googleplaces");
    const places = new GooglePlaces(API_KEY);
    //const params = {
    //  location: '40.689247,-123.102192',
    //  radius: 1000
    //};

    return places.textSearch({ query: name_of_place })
        .then(res => res.body.results[0].place_id)
        .then(placeID => places.details({ placeid : placeID }))
        .then(res => res.body.result.price_level)
}

Upvotes: 4

ralphpig
ralphpig

Reputation: 99

You're not declaring a promise correctly, return new Promise( function(resolve, reject){}

And then you can use that like so

places.details(request_place_details).then((res) => {
             console.log(res.body.result.price_level + "   S");
             var cost = res.body.result.price_level;


             // Just resolve it here.
             resolve(cost);                 
        });


// And here, res == cost.
Event1.getCost('Thai Moon').then(res=> {
  console.log(res);
})

Also for reference, check out the Promise docs.

Upvotes: 0

Related Questions