0
  searchOrder.callApiBasedOnSearchBy = function(searchBy,responseObj) {
    var recordsFinalObj = {
      recordsArray: [],
      isOrderList: false
    };
    var finalJson = {};
    var self = this;
    var mapSearchByToApi = {
      'INVOICE_NUMBER': function(responseObj) {
      },
      'ORDER_NUMBER': function(responseObj) {
        self.orderDetailsModel.set("id",responseObj.id);
        self.orderDetailsModel.fetch({}).then(function(orderDetailsResponse) {
          var orderDetails = orderDetailsResponse.records[0];
          if(typeof orderDetails.packet_ids !== 'undefined') {
            var packet_id = orderDetails.packet_ids[0];
            self.packetDetailsModel.set("id",packet_id);
            self.packetDetailsModel.fetch({}).then(function(packetDetailsResponse) {
              console.log(packetDetailsResponse);
              finalJson = packetDetailsResponse.records[0];
              finalJson.order_items = orderDetails.order_items;
              console.log(finalJson);
              return finalJson;
            }, function(error) {
              console.log("Error in Order Item:"+error);
            });

          }
        }, function(error) {
          console.log("Error in Order:"+error);
        })
      },
      'AWB_NUMBER': function(responseObj) {

      },
      'CHANNEL_ORDER_ID': function(responseObj) {

      }
    }

    if(mapSearchByToApi[searchBy]) {
      finalJson = mapSearchByToApi[searchBy](responseObj);
      recordsFinalObj.recordsObj = finalJson;
      return recordsFinalObj;
    }
  }

I want to return the final json to my UI. However the control of the loop is returned before my promises are executed.

vini
  • 4,657
  • 24
  • 82
  • 170
  • A promise is a return value (a promise of a future result), not a function, and doesn't "execute". Therefore, you should return them. `mapSearchByToApi.ORDER_NUMBER` returns `undefined` right now. Also, where's the loop? – jib Mar 18 '16 at 11:46
  • It'll be better if you write a simple function that does something asynchronous and try returning something from it, see what happens and handle that situation first, rather than writing a pile of code that doesn't work an then posting it on stackoverflow saying it doesn't work... – T J Mar 18 '16 at 12:25
  • I must be missing something, but where's a loop in that code? – Bergi Mar 18 '16 at 13:06
  • You *always* need to `return` a promise from functions (specifically `then` callback functions) if you're doing something asynchronous. – Bergi Mar 18 '16 at 13:07

1 Answers1

0

In asyc functions or functions that call async procedures you have to either use return promises or use callbacks to capture errors and data.

searchOrder.callApiBasedOnSearchBy = function(searchBy,responseObj) {
    var recordsFinalObj = {
        recordsArray: [],
        isOrderList: false
    };
    var finalJson = {};
    var self = this;
    var mapSearchByToApi = {
        'INVOICE_NUMBER': function(responseObj) {
        },
        'ORDER_NUMBER': function(responseObj) {
            // Return a promise instead of trying to return data itself
            return new Promise(function(resolve, reject) { self.orderDetailsModel.set("id",responseObj.id);
                self.orderDetailsModel.fetch({}).then(function(orderDetailsResponse) {
                    var orderDetails = orderDetailsResponse.records[0];
                    if(typeof orderDetails.packet_ids !== 'undefined') {
                        var packet_id = orderDetails.packet_ids[0];
                        self.packetDetailsModel.set("id",packet_id);
                        self.packetDetailsModel.fetch({}).then(function(packetDetailsResponse) {
                            console.log(packetDetailsResponse);
                            finalJson = packetDetailsResponse.records[0];
                            finalJson.order_items = orderDetails.order_items;
                            console.log(finalJson);
                            return resolve(finalJson); // resolve ur data
                        }, function(error) {
                            console.log("Error in Order Item:"+error);
                            reject(error);
                        });
                    }
                }, function(error) {
                    console.log("Error in Order:"+error);
                });
            });

        },
        'AWB_NUMBER': function(responseObj) {

        },
        'CHANNEL_ORDER_ID': function(responseObj) {

        }
    };

    if(mapSearchByToApi[searchBy]) {
        // Have to return a promise
        return new Promise(function(resolve, reject) {
            mapSearchByToApi[searchBy](responseObj)
                .then(function(finalJson) {
                    recordsFinalObj.recordsObj = finalJson;
                    resolve(recordsFinalObj);
                })
                .catch(function(err){
                    reject(err);
                }) ;
        });

    }
};
Deepak Puthraya
  • 1,325
  • 2
  • 17
  • 28