我試圖建立和使用藍鳥承諾返回一個對象的數組。 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;
});
};
我無法弄清楚如何從嵌套承諾返回值