[英]Convert array of images to base64 using readAsStringAsync in React Native
I have the following code:我有以下代码:
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,
}
})
})
The issue is that the method readAsStringAsync is Async, but I cant get to work with await.问题是 readAsStringAsync 方法是异步的,但我无法使用 await。 I tried to call another async function to make this method and wait for it but its not possible.
我试图调用另一个异步函数来创建这个方法并等待它,但这是不可能的。
The question is... how can I wait to an async function to complete inside a map?问题是......我如何才能等待异步函数在地图内完成? I dont want to go to next iteration until the async is complete.
在异步完成之前,我不想进行下一次迭代。
Thanks.谢谢。
Promise.all async function within map callback is easier to implement:地图回调中的 Promise.all 异步函数更容易实现:
let response=await Promise.all(data.People.map((item, index)=>{让响应=等待 Promise.all(data.People.map((item, index)=>{
//Your code //你的代码
})) }))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.