我在这里链接承诺:
我期待.then调用仅在前一个调用完成后才会发生。但相反,在发送验证电子邮件或更新配置文件之前,会发生重定向。下面是负责相同的代码:
function authorizeWithFireBase(email,displayName,password,photoURL){
var user = null;
var failText = document.getElementById("signup-invalid-message");
//NULLIFY EMPTY ARGUMENTS
for (var i = 0; i < arguments.length; i++) {
arguments[i] = arguments[i] ? arguments[i] : null;
}
auth.createUserWithEmailAndPassword(email, password)
.then(function () {
user = auth.currentUser;
user.sendEmailVerification();
})
.then(function () {
user.updateProfile({
displayName: displayName,
photoURL: photoURL
});
})
.then(function () {
window.location.replace("/path/to/private/app/dashboard/page");
})
.catch(function(error) {
if(error.code == 'auth/weak-password') {
failText.innerHTML = "Please set a complex password!"
}
else {
failText.innerHTML = error.message;
}
});
console.log('Validation link was sent to ' + email + '.');
}
我尝试切换
.then(function(){...})
到
.then(()=&gt; function(){...})
但是3 .then()部分都没有被执行。
我做错了什么?我如何将这些承诺链接到上述所有3个步骤?
答案 0 :(得分:1)
您必须在每个then()
内返回一个承诺,以便能够将它们链接起来。幸运的是你sendEmailVerification()
和updateProfile()
都返回了承诺,所以这是一个简单的改变:
auth.createUserWithEmailAndPassword(email, password)
.then(function () {
user = auth.currentUser;
return user.sendEmailVerification();
})
.then(function () {
return user.updateProfile({
displayName: displayName,
photoURL: photoURL
});
})
.then(function () {
window.location.replace("/path/to/private/app/dashboard/page");
})
.catch(function(error) {
if(error.code == 'auth/weak-password') {
failText.innerHTML = "Please set a complex password!"
}
else {
failText.innerHTML = error.message;
}
});