2017-05-26 30 views
0

我在查詢airtable的一些數據,這些數據基本上是書籍和相關作者的數據庫。每本書籍都有一個authorId字段,我必須單獨查詢才能獲取相關的作者數據。我如何異步同時等待多個承諾? [語法錯誤:await是保留字]

這裏是我得到的所有的書第一:

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}` 
    ]))) 
} 

這個函數應該讓我知道我的相關作者數據,但是我得到了一個呃ROR:

Syntax Error: await is a reserved word 

...在這條線:authorIds.map(id => await Promise.all([...

我在做什麼錯的,是有辦法解決這一問題?

+0

我的巴貝爾設置工作正常,我使用了預先配置用於翻譯等的next.js。此外,第一次請求書的作品很好,這意味着異步不是問題 –

+0

更新了我得到錯誤的行:'authorIds .map(id =>等待Promise.all([' –

+0

爲什麼你需要async/await + promises?這是兩種不同的模式來管理異步代碼。 – Booster2ooo

回答

4

您已將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

相關問題