如何在promise javascript中返回值
我的代码有一些问题。我需要在承诺中回报价值,但不知道如何实现。我是ECS6的新手 以下是我的createDate函数:如何在promise javascript中返回值,javascript,asynchronous,ecmascript-6,bluebird,Javascript,Asynchronous,Ecmascript 6,Bluebird,我的代码有一些问题。我需要在承诺中回报价值,但不知道如何实现。我是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)
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**
}
});
})
})
})
})
})
createData(0, json).then(function(rs) {
console.log(rs)
**It not console anything because I think createData not return or resolve anything**
})
你需要把你的承诺连成链,然后每个
都应该返回它里面的承诺。此外,避免:
var createData=函数(i,对象){
变量项=对象[i]
var desease=handleDiease(item.disease);//发出所有请求
var country=handleCountry(item.country);//同时,无需等待
var stat=handleStatus(lodash.capitalize(条目['status(否/尝试失败/是/朋友)'));
变量类型=handleType(lodash.capitalize(项目[“类型(o/p)”]))
//加入几个承诺,注意这里的'return'。
返回承诺。加入(疾病、国家、统计、类型、,
函数(deseaseId、countryId、statusId、typeId){
return contactbl.create({//这也需要一个'return'
名称:item.name,
组织:item.organization,
电子邮件:item.email,
电话号码:item.phone,
facebook\u url:item.facebook\u url,
联系日期:item.date,
da_名称:item.generation_附属公司名称,
da_url:item.generation_affiliate_url,
article\u url:item.article\u url,
//isparticipattefacp:item.isparticipattefacp,
调查编号:item.survey,
注:项目.注:,
fbcontact_diseaseid:diseaseid,
FBU联系人countryid:countryid,
lk_contactstatusid:statusId,
lk_contacttypeid:typeId,
});
})
.然后(函数(rs){//
如果(i
谢谢您的帮助。但是当我使用这个函数时,它可能不起作用:var abc=createDate(0,json)=>console.log(abc)=null。你能支持我使用吗?此函数的目的是从csv文件中插入日期,插入每一行,然后转到检查数组的if条件,if have else将回调函数:)
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;
});
};