【问题标题】:How to return value in promise javascript如何在承诺javascript中返回值
【发布时间】:2015-10-07 08:11:44
【问题描述】:

我的代码有问题。我需要在承诺中返回一个值,但不知道如何实现。我是 ECS6 的新手

以下是我的 createDate 函数:

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

}

以下是我使用 createDate 函数的地方:

createData(0, json).then(function(rs) {
  console.log(rs)
  **It not console anything because I think createData not return or resolve anything**
})

【问题讨论】:

    标签: javascript asynchronous ecmascript-6 bluebird


    【解决方案1】:

    你需要链接你的promise,每个then应该return里面的promise。另外,避免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;
      });
    };
    

    【讨论】:

    • 感谢您的帮助。但是当我使用这个函数时它可能不起作用:var abc = createDate(0, json) => console.log(abc) = null。你能支持我使用吗?这个函数的目的是从csv文件中插入日期,插入每一行然后去if条件检查数组,如果有else会回调函数:)
    猜你喜欢
    • 2019-11-02
    • 1970-01-01
    • 1970-01-01
    • 2023-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-19
    相关资源
    最近更新 更多