use*_*704 2 javascript asynchronous node.js es6-promise
我有一个关于人的数据对象数组。每个人员对象都包含0-n个URL,以获取其他信息(人员的来宾)。
我要处理此列表,调用每个“来宾” URL,并在原始数据集中包括来宾的名字。
上下文:这是一个AWS Lambda函数。我正在使用lambda-local在本地运行。(lambda-local -l index.js -e fixtures/test_event1.json
)。
我已成功使用await / async来检索初始数据集。
但是我无法获得有关来宾信息的更多电话服务。即使结果等待,它始终显示一个未决的Promise。
// index.js
const fetch = require('node-fetch');
exports.handler = async function(event){
try {
let registrations = await getEventRegistrations();
console.log(registrations);
/* All good here - sample console output
[ { contactId: 43452967,
displayName: 'aaa, bbb',
numGuests: 0,
guestUrls: [] },
{ contactId: 43766365,
displayName: 'bbb, ccc',
numGuests: 1,
guestUrls:
[ 'https://<URL>' ] },
{ contactId: 43766359,
displayName: 'ccc, ddd',
numGuests: 2,
guestUrls:
[ 'https://<URL>',
'https://<URL> ] } ]
*/
// Expanding the guest URLs is proving problematic - see expandGuests function below
registrations = registrations.map(expandGuests);
console.log(registrations);
/* Registrations are just pending Promises here, not the real data
[ Promise { <pending> },
Promise { <pending> },
Promise { <pending> } ]
*/
return {
statusCode: 200,
headers: {
'Access-Control-Allow-Origin': '*',
},
body: JSON.stringify(registrations),
};
}
catch (exception) {
console.log(exception);
return {
statusCode: 500,
body: 'Unable to retrieve data.'
};
}
};
function getEventRegistrations() {
return fetch('<URL>')
.then(res => res.json())
.catch(function (error) {
console.log('Event registrants request failed', error);
return null;
});
}
function getGuestName(url) {
return fetch(url)
.then(res => res.json())
.then(guest => guest.DisplayName)
.catch(function (error) {
console.log('Guest name request failed', error);
return null;
});
}
async function expandGuests(data) {
const promises = data.guestUrls.map(url => getGuestName(url));
data.guestNames = await Promise.all(promises);
return data;
}
Run Code Online (Sandbox Code Playgroud)
如何解决这些待处理的承诺,从而返回有用的数据?
谢谢。
array.map不包含任何等待诺言的逻辑。它只是为数组的每个元素调用函数,然后同步生成结果数组。如果传入异步函数,则结果将是一个Promise数组(因为所有异步函数都返回Promise)。
您将需要使用Promise.all
创建一个承诺,该承诺将在所有单个承诺都解决后再解决await
。
const promises = data.guestUrls.map(url => getGuestName(url));
data.guestNames = await Promise.all(promises);
Run Code Online (Sandbox Code Playgroud)
PS,我强烈建议不要将.athen与async / await混合使用。实际需要这样做的情况非常少见,因此在大多数情况下,您只是会使代码难以理解。例如,此功能:
async function getGuestName(url) {
return await fetch(url)
.then(res => res.json())
.then(guest => guest.DisplayName)
.catch(function (error) {
console.log('Guest name request failed', error);
return null;
});
}
Run Code Online (Sandbox Code Playgroud)
应该这样进行:
async function getGuestName(url) {
try {
const res = await fetch(url);
const guest = await res.json();
return guest.DisplayName;
} catch (error) {
console.log('Guest name request failed', error);
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
或者像这样
function getGuestName(url) {
return fetch(url)
.then(res => res.json())
.then(guest => guest.DisplayName)
.catch(function (error) {
console.log('Guest name request failed', error);
return null;
});
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
117 次 |
最近记录: |