如何 .then 链接但需要先前承诺的 resolve() 值 - javascript?
How to .then chain but need the resolve() value from the previous promise - javascript?
问题出在"backendLoginCheck"函数上。我想 .then() 在 "getUserByEmail" 之后链接 "ifUserIsDisabled"。但是我需要来自 getUserByEmail 的 "userRecord" 来输入 "ifUserIsDisabled".
我还希望两个函数在 "backendLoginCheck" 函数中共享同一个 .catch。
当前代码:
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);
});
}
想要的想法:
...
getUserByEmail(email)
.then(ifUserIsDisabled(userRecord))
.then(nextFunction())
.then(nextFunction2(uses_resolveVal_from_nextFunction))
.then(nextFunctionEtc())
.catch(function(error){
callback(error);
});
应该是这样的:
getUserByEmail(email)
.then(function(userRecord){
return ifUserIsDisabled(userRecord);
})
.then(nextFunction())
如果我对你的理解是正确的,看起来你就快完成了。如果一个 then 链在一个 promise returns 另一个 promise 中,你可以将解析后的值传递到链中
例如:
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)
})
问题出在"backendLoginCheck"函数上。我想 .then() 在 "getUserByEmail" 之后链接 "ifUserIsDisabled"。但是我需要来自 getUserByEmail 的 "userRecord" 来输入 "ifUserIsDisabled".
我还希望两个函数在 "backendLoginCheck" 函数中共享同一个 .catch。
当前代码:
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);
});
}
想要的想法:
...
getUserByEmail(email)
.then(ifUserIsDisabled(userRecord))
.then(nextFunction())
.then(nextFunction2(uses_resolveVal_from_nextFunction))
.then(nextFunctionEtc())
.catch(function(error){
callback(error);
});
应该是这样的:
getUserByEmail(email)
.then(function(userRecord){
return ifUserIsDisabled(userRecord);
})
.then(nextFunction())
如果我对你的理解是正确的,看起来你就快完成了。如果一个 then 链在一个 promise returns 另一个 promise 中,你可以将解析后的值传递到链中
例如:
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)
})