我正在从 airtable 查询一些数据这基本上是一个书籍和相关作者的数据库。每本书记录都有一个 authorId
字段,我必须单独查询该字段以获取相关的作者数据。
以下是我首先获取所有书籍的方法:
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] )
这有效,我得到了这些作者 ID:
[ 'recNLaQrmpQzfkOZ1',
'recmDfVxRp01x85F9',
'recKQqdJ9a2pHnF2z',
'recKMiDhdCUxfdPSY',
'rec67WoUDFjFMrw44' ]
现在我想将此数据发送到 getAuthors
函数,如下所示:
const getAuthors = async (authorIds) => {
authorIds.map( id => await Promise.all([
return axios.get(`https://api.airtable.com/v0/appGI25cdNsGR2Igq/Authors/${id}`
])))
}
此函数应该为我提供相关的作者数据,但我收到了错误:
Syntax Error: await is a reserved word
...这一行:authorIds.map( id => wait Promise.all([...
我做错了什么,有办法解决这个问题吗?
最佳答案
您已将 await
放置在 map
回调函数中,而不是声明为 async
的回调函数中。您会想要使用
async function getAuthors(authorIds) {
await Promise.all(authorIds.map(id =>
axios.get(`https://api.airtable.com/v0/appGI25cdNsGR2Igq/Authors/${id}`)
));
}
不过可能最好用 return
替换 await
。
关于javascript - 如何并行异步等待多个 promise ? [语法错误: await is a reserved word],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44197075/