如果您想在代码中不等待Google OAuth的承诺,您可以使用async/await和try/catch结构来处理异步操作。下面是一个示例:
async function signInWithGoogle() {
try {
const user = await gapi.auth2.getAuthInstance().signIn();
// 根据需要执行其他操作,例如获取用户详细信息等
console.log("成功登录Google账号", user);
} catch (error) {
console.error("Google登录失败", error);
}
}
// 在需要的地方调用signInWithGoogle函数
signInWithGoogle();
在上面的代码中,signInWithGoogle
函数使用await
关键字来等待Google OAuth的异步操作完成。如果操作成功,将返回一个用户对象,您可以在其中执行其他操作。如果操作失败,将会抛出一个错误,您可以使用catch
块来捕获并处理该错误。
请注意,上述代码假设您已经通过适当的方式加载和初始化了Google API。