[英]Convert array of images to base64 using readAsStringAsync in React Native
我有以下代碼:
export const blSaveRecord = async (data) => {
const params = new URLSearchParams()
params.append('DocumentNumber', data.DocNumber)
data.People.map((item, index) => {
params.append(`Person[${index}].PersonDoc`, item.PersonDoc)
params.append(
`Person[${index}].Document[0].Base64File`,
`data:image/jpg;base64,${FileSystem.readAsStringAsync(item.image, {
encoding: FileSystem.EncodingType.Base64,
})}`
)
})
//THIS IS THE ISSUE:
console.log(params)
return axios
.post(url_SaveRecord, params)
.then(function (response) {
return {
status: true,
message: '',
}
})
.catch(function (error) {
return {
status: false,
message: error.message,
}
})
})
問題是 readAsStringAsync 方法是異步的,但我無法使用 await。 我試圖調用另一個異步函數來創建這個方法並等待它,但這是不可能的。
問題是......我如何才能等待異步函數在地圖內完成? 在異步完成之前,我不想進行下一次迭代。
謝謝。
地圖回調中的 Promise.all 異步函數更容易實現:
讓響應=等待 Promise.all(data.People.map((item, index)=>{
//你的代碼
}))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.