Javascript 等待映射内的所有异步函数完成执行
我想完成映射中所有异步函数的执行,然后更改组件的状态。我给你留了一部分代码,以备你能帮我 我只是想在每次用户取消时显示我自己的权限屏幕 简单来说,我有以下几点:Javascript 等待映射内的所有异步函数完成执行,javascript,reactjs,react-native,async-await,react-redux,Javascript,Reactjs,React Native,Async Await,React Redux,我想完成映射中所有异步函数的执行,然后更改组件的状态。我给你留了一部分代码,以备你能帮我 我只是想在每次用户取消时显示我自己的权限屏幕 简单来说,我有以下几点: const permissions = { cameraRoll: { iconType: "ionicon", iconName: "ios-images", title: "Enable camera roll",
const permissions = {
cameraRoll: {
iconType: "ionicon",
iconName: "ios-images",
title: "Enable camera roll",
subtitle:
"To upload content from your Gallery, you have to granted the camera roll permission",
buttonText: "Enable camera roll",
checkPermission: checkCameraRollPermission,
requirePermission: requireCameraRollPermission,
},
};
}
const checkCameraRollPermission = async () => {
const { status, canAskAgain } = await Permissions.getAsync(
Permissions.CAMERA_ROLL
);
return { status, canAskAgain };
};
const requireCameraRollPermission = async () => {
const { status, canAskAgain } = await Permissions.askAsync(
Permissions.CAMERA_ROLL
);
return { status, canAskAgain };
};
/* I HAVE TO WAIT FOR ALL THE FUNCTIONS TO FINISH EXECUTION
FOR EACH OBJECT ON THE MAP BUT THIS IS NOT WORKING :( */
getMissingPermissions = () => {
// Only check permissions on the foreground
if (AppState.currentState.match(/active/)) {
// We have to empty the current missing permssions and recalculate them
const permissionsArray = [];
Promise.all(
Object.keys(permissions).map((key) => {
permissions[key].checkPermission().then(({ status }) => {
if (status !== "granted") {
permissionsArray.push(permissions[key]);
}
});
})
).then(() => {
this.setState({
missingPermissions: permissionsArray,
});
});
}
};
有什么想法吗?感谢与
承诺。您需要以数组形式提供承诺。目前,您没有从地图中返回导致问题的任何内容
也可以不使用permissionsArray变量,只需从嵌套的promise中返回所需的值,该值将在promise.all(…)。然后(…)
响应中可用
getMissingPermissions = () => {
// Only check permissions on the foreground
if (AppState.currentState.match(/active/)) {
// We have to empty the current missing permssions and recalculate them
Promise.all(
Object.keys(permissions).map((key) => {
return permissions[key].checkPermission().then(({ status }) => {
if (status !== "granted") {
return permissions[key];
}
return;
});
})
).then((res) => {
const permissionsArray = res.filter(Boolean)// Filter out undefined values
this.setState({
missingPermissions: permissionsArray,
});
});
}
};
.map()
的回调应该返回一个值现在您的权限
对象只有一个请求的权限。它最终会有不止一个吗?如果没有,那么你甚至不需要映射权限调用。是的,它有更多,它被简化了