[英]How do I async await multiple promises in parallel? [Syntax Error: await is a reserved word]
I am querying some data from airtable which is basically a database of books and related authors. 我正在查询airtable的一些数据,该数据基本上是书籍和相关作者的数据库。 Each book record has an
authorId
field which I have to query separately to fetch related author data. 每本书记录都有一个
authorId
字段,我必须单独查询该字段以获取相关的作者数据。
Here is how I get all books first: 这是我首先获得所有书籍的方式:
let books = await axios.get("https://api.airtable.com/v0/appGI25cdNsGR2Igq/Books?&view=Main%20View")
let authorIds = books.data.records.map( ( book) => book.fields.Author[0] )
This works and I get these author ids: 这有效,我得到了这些作者ID:
[ 'recNLaQrmpQzfkOZ1',
'recmDfVxRp01x85F9',
'recKQqdJ9a2pHnF2z',
'recKMiDhdCUxfdPSY',
'rec67WoUDFjFMrw44' ]
Now I want to send this data to a getAuthors
function like this: 现在,我想将此数据发送到这样的
getAuthors
函数:
const getAuthors = async (authorIds) => {
authorIds.map( id => await Promise.all([
return axios.get(`https://api.airtable.com/v0/appGI25cdNsGR2Igq/Authors/${id}`
])))
}
This function is supposed to get me my related author data but instead I get an error: 该功能应该可以获取我相关的作者数据,但会出现错误:
Syntax Error: await is a reserved word
...on this line: authorIds.map( id => await Promise.all([...
...在此行上:
authorIds.map( id => await Promise.all([...
What am I doing wrong and is there a way to fix this? 我做错了什么,有办法解决吗?
You have placed the await
in the map
callback function, not in the one that is declared as async
. 您已将
await
放置在map
回调函数中,而不是在声明为async
函数中。 You'll want to use 您将要使用
async function getAuthors(authorIds) {
await Promise.all(authorIds.map(id =>
axios.get(`https://api.airtable.com/v0/appGI25cdNsGR2Igq/Authors/${id}`)
));
}
Though probably better replace the await
by return
. 虽然可能最好用
return
代替await
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.