好吧,我研究了你的代码,但你所做的不叫承诺,你客观地给函数一个回调,而不是使用.then来解决承诺。
检查 GetMacAddress here 的 API 并且 getCurrentPosition 不使用承诺,而是使用回调,api 在这里。
使用Promise.all 可以做的是,您可以将getCurrentPosition 包装到一个promise 中,然后将promise.all 与DeviceInfo 一起使用。
将getposition 包装成一个承诺:
const getPosition = (options) => {
return new Promise((resolve, reject) => {
navigator.geolocation.getCurrentPosition(resolve, reject, options);
});
}
现在像这样使用Promise.all:
const getData = async () => {
const [macAddress, currentPos] = await Promise.all([
DeviceInfo.getMACAddress(),
getPosition(),
]);
// use macAddress and currentPosition here.
}
现在macAddress 和currentPos 是getMACAddress 和getCurrentPosition 函数的输出。
如果您不使用异步函数,则可以执行以下操作:
Promise.all([
DeviceInfo.getMACAddress(),
getPosition(),
]).then((macAddress, pos) => {
// access macAddress and pos in this func
}).catch((error) => {
// access any error here
})