"Can not use 'await' outside an async function"- React Native 文档出错
"Can not use 'await' outside an async function"- Error on React Native Docs
我是 React Native 的新手。我正在制作这个需要打开 WiFi 的应用程序。我为 wifi 使用了 npm i react-native-wifi-reborn
包。在尝试该网页提供的示例时出现错误
不能在异步函数外使用'await'
我复制了相同的代码,仍然遇到这个错误。
代码
import { PermissionsAndroid } from 'react-native';
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
{
title: 'Location permission is required for WiFi connections',
message:
'This app needs location permission as this is required ' +
'to scan for wifi networks.',
buttonNegative: 'DENY',
buttonPositive: 'ALLOW',
},
);
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
// You can now use react-native-wifi-reborn
} else {
// Permission denied
}
点击Here查看网站。
开发人员希望您使用 top-level async/await,或者更可能是希望您正确处理异步调用的简单示例:
const getLocationPermission = async () => {
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
{
title: 'Location permission is required for WiFi connections',
message: 'This app needs location permission as this is required to scan for wifi networks.',
buttonNegative: 'DENY',
buttonPositive: 'ALLOW',
},
);
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
// You can now use react-native-wifi-reborn
} else {
// Permission denied
}
} catch (e) {
// Handle unexpected error
}
}
我不希望 React Native 处理顶级 async/await 因此您必须将对 PermissionsAndroid.request 的调用包装在异步函数中。
错误自行解释。您必须在 async 函数中使用 await 。
试试这个:
const isPermissionGranted = async() => {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
{
title: 'Location permission is required for WiFi connections',
message:
'This app needs location permission as this is required ' +
'to scan for wifi networks.',
buttonNegative: 'DENY',
buttonPositive: 'ALLOW',
},
);
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
return true;
} else {
return false;
}
}
我是 React Native 的新手。我正在制作这个需要打开 WiFi 的应用程序。我为 wifi 使用了 npm i react-native-wifi-reborn
包。在尝试该网页提供的示例时出现错误
不能在异步函数外使用'await'
我复制了相同的代码,仍然遇到这个错误。
代码
import { PermissionsAndroid } from 'react-native';
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
{
title: 'Location permission is required for WiFi connections',
message:
'This app needs location permission as this is required ' +
'to scan for wifi networks.',
buttonNegative: 'DENY',
buttonPositive: 'ALLOW',
},
);
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
// You can now use react-native-wifi-reborn
} else {
// Permission denied
}
点击Here查看网站。
开发人员希望您使用 top-level async/await,或者更可能是希望您正确处理异步调用的简单示例:
const getLocationPermission = async () => {
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
{
title: 'Location permission is required for WiFi connections',
message: 'This app needs location permission as this is required to scan for wifi networks.',
buttonNegative: 'DENY',
buttonPositive: 'ALLOW',
},
);
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
// You can now use react-native-wifi-reborn
} else {
// Permission denied
}
} catch (e) {
// Handle unexpected error
}
}
我不希望 React Native 处理顶级 async/await 因此您必须将对 PermissionsAndroid.request 的调用包装在异步函数中。
错误自行解释。您必须在 async 函数中使用 await 。 试试这个:
const isPermissionGranted = async() => {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
{
title: 'Location permission is required for WiFi connections',
message:
'This app needs location permission as this is required ' +
'to scan for wifi networks.',
buttonNegative: 'DENY',
buttonPositive: 'ALLOW',
},
);
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
return true;
} else {
return false;
}
}