[英]How to .then chain but need the resolve() value from the previous promise - javascript?
The issue lies in the "backendLoginCheck" function. 问题在于“ backendLoginCheck”功能。 I want to .then() chain "ifUserIsDisabled" after "getUserByEmail".
我想在“ getUserByEmail”之后将.then()链“ ifUserIsDisabled”。 But I need the "userRecord" from getUserByEmail for the input into "ifUserIsDisabled".
但是我需要getUserByEmail的“ userRecord”作为“ ifUserIsDisabled”的输入。
I also want both functions to share the same .catch in "backendLoginCheck" function. 我也希望两个函数在“ backendLoginCheck”函数中共享相同的.catch。
Current Code: 当前代码:
function getUserByEmail(email){
return new Promise(function(resolve, reject){
firebase.serverAuthAdmin147th.getUserByEmail(email)
.then(function(userRecord) {
resolve(userRecord);
})
.catch(function (error) {
reject({token: null, errorCode: "auth/user-not-found"});
});
})
}
function ifUserIsDisabled(userRecord){
return new Promise(function(resolve, reject){
if(!userRecord.disabled){
resolve();
}
else{
reject({token: null, errorCode: "auth/user-disabled"});
}
})
}
function backendLoginCheck(email, password, callback){
var token = null;
var errorCode = null;
var uid = null;
getUserByEmail(email)
.then(function(userRecord){
ifUserIsDisabled(userRecord);
})
.catch(function(error){
callback(error);
});
}
Desired Idea: 期望的主意:
...
getUserByEmail(email)
.then(ifUserIsDisabled(userRecord))
.then(nextFunction())
.then(nextFunction2(uses_resolveVal_from_nextFunction))
.then(nextFunctionEtc())
.catch(function(error){
callback(error);
});
should be like this: 应该是这样的:
getUserByEmail(email)
.then(function(userRecord){
return ifUserIsDisabled(userRecord);
})
.then(nextFunction())
If I'm understanding you correctly, it looks like you're almost there. 如果我正确地理解了您,则好像您快到了。 If a then chain in a promise returns another promise you can pass the resolved values down the chain
如果一个promise中的then链返回另一个promise,您可以将已解析的值向下传递
for example: 例如:
function firstFunction() {
return new Promise((resolve, reject) => {
if (someError) {
reject(firstErr)
} else {
resolve('first value')
}
})
}
function secondFunction() {
return new Promise((resolve, reject) => {
if (someError) {
reject(secondErr)
} else {
resolve('second value')
}
})
}
firstFunction()
.then((resolvedFirstValue) => {
return secondFunction()
})
.then((resolvedSecondValue) => {
console.log(resolvedSecondValue)
})
.catch((err) => {
// any error in the entire chain
console.error(err)
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.