我试图建立和使用蓝鸟承诺返回一个对象的数组。 Promise是一个HTTP请求,它获取要添加到对象的附加数据。在For循环使用蓝鸟承诺建立并返回对象
我创建的执行for循环的请求的功能(我还使用了一个框架,进行一些中间件 - 这是z.
是关于什么的)
const getWebAppCustomFieldDetails = (z, url) => {
const responsePromise = z.request({
url:url,
headers:{
'content-type': 'application/json'
}
});
return responsePromise
.then(response =>{
return JSON.parse(response.content);
});
};
该函数中调用下面的代码:
const webAppFields = (z, bundle) => {
//This section carries creates an initial request which gets the bulk of the data
const responsePromise = z.request({
url: webAppUrl(bundle) + '/' + encodeURI(bundle.inputData.webApp),
headers:{
'content-type': 'application/json'
},
});
//This is an array to hold the objects created from the response
var fields = [];
return responsePromise
.then(response => {
response = JSON.parse(response.content);
//From the response, append the core fields
response.systemFields.forEach(function (systemField) {
fields.push({
'key': systemField.name,
'required': systemField.required,
'type': systemField.type.toLowerCase()
});
});
return response;
})
.then(response => {
//Sometimes there are custom fields that need to be retrieved individually
const customFieldCount = response.fields.length;
var customFieldAppend = '';
for (var i = 0; i < customFieldCount; i++){
getWebAppCustomFieldDetails(z, response.fields[0].links[0].uri)
.then(response =>{
customFieldAppend = {
'key': response.name,
'required': response.required,
'type': response.type.toLowerCase()
};
//This push doesn't updated the fields array!
fields.push(customFieldAppend);
});
}
//This return does not include the custom fields!
return fields;
});
};
我无法弄清楚如何从嵌套承诺返回值