循环超出范围而不执行承诺链

时间:2016-03-18 10:35:46

标签: javascript backbone.js promise

  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;
    }
  }

我想将最终的json返回到我的UI。但是,在我的承诺执行之前,会返回循环控制。

1 个答案:

答案 0 :(得分:0)

在调用异步过程的asyc函数或函数中,您必须使用return promises或使用回调来捕获错误和数据。

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

    }
};