2

I have some issue with my code. I need to return a value in promise but don't know how to achived that. I'm newbie in ECS6

Following is my createDate function:

var createData = function (i, object) {
return new Promise(function(resolve) {
  var item = object[i]
  handleDiease(item.disease).then(function (diseaseId) {
    handleCountry(item.country).then(function (countryId) {
      handleStatus(lodash.capitalize(item['status(no/failed attempt/yes/friend)'])).then(function (statusId) {
        handleType(lodash.capitalize(item["type(o/p)"])).then(function (typeId) {
          ContactBLL.create({
            name: item.name,
            organisation: item.organisation,
            email: item.email,
            phonenumbers: item.phone,
            facebook_url: item.facebook_url,
            contactdate: item.date,
            da_name: item.donation_affiliate_name,
            da_url: item.donation_affiliate_url,
            article_url: item.article_url,
            //isparticipatefacp: item.isparticipatefacp,
            survey_id: item.survey,
            notes: item.notes,
            fbcontact_diseaseid: diseaseId,
            fbcontact_countryid: countryId,
            lk_contactstatusid: statusId,
            lk_contacttypeid: typeId,
          }).then(function (rs) {
            if (i < object.length - 2) createData(i + 1, object)
            else {
              **In else case, i want to return value, i'm using resolve(true) or return true but bold of them not work**
            }
          });
        })
      })
    })
  })
})

}

Following is where I use createDate function:

createData(0, json).then(function(rs) {
  console.log(rs)
  **It not console anything because I think createData not return or resolve anything**
})
Dyrandz Famador
  • 4,499
  • 5
  • 25
  • 40
Trí Đỗ
  • 23
  • 4

1 Answers1

3

You need to chain your promises, each then should return the promise inside it. Also, avoid explicit construction:

var createData = function (i, object) {
  var item = object[i]
  var desease = handleDiease(item.disease); // make all requests 
  var country = handleCountry(item.country); // concurrently, no need to wait
  var stat = handleStatus(lodash.capitalize(item['status(no/failed attempt/yes/friend)']));
  var type = handleType(lodash.capitalize(item["type(o/p)"]))
  // join aggregates several promises, notice the `return` here.
  return Promise.join(desease, country, stat, type, 
         function(deseaseId, countryId, statusId, typeId) { 
    return ContactBLL.create({ // this needs a `return` too
      name: item.name,
      organisation: item.organisation,
      email: item.email,
      phonenumbers: item.phone,
      facebook_url: item.facebook_url,
      contactdate: item.date,
      da_name: item.donation_affiliate_name,
      da_url: item.donation_affiliate_url,
      article_url: item.article_url,
      //isparticipatefacp: item.isparticipatefacp,
      survey_id: item.survey,
      notes: item.notes,
      fbcontact_diseaseid: diseaseId,
      fbcontact_countryid: countryId,
      lk_contactstatusid: statusId,
      lk_contacttypeid: typeId,
    });
  })
  .then(function (rs) { // chain the promise
      if (i < rs.length - 2) return createData(i + 1, rs); 
      else return true;
  });
};
Community
  • 1
  • 1
Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
  • thank for your helping. But it may be not working when I use this function: var abc = createDate(0, json) => console.log(abc) = null. Can you support me to use? The purpose of this function is insert date from csv file, insert each row and then go to if condition for checking array, if have else will be call back the function :) – Trí Đỗ Oct 07 '15 at 08:48