Passport JS 和自定义 async/await 函数
Passport JS and custom async/await function
我正在使用 Passport JS 本地策略,并想添加一个自定义函数来对用户提供的电子邮件进行一些验证
async function checkEmail(email) {
const url = `https://endpoint-to-url/${email}`;
await fetch(url)
.then(response => response.json())
.then(async json => {
let result;
if (json.status === 200 && json.temporary === true) {
result = true;
} else {
result = false;
}
return result;
})
.catch(async function(error) {
console.log(`Error whilst checking email ${error}`);
});
}
护照 JS
passport.use(
'local-signup',
new LocalStrategy(
{
usernameField: 'signup_email',
passwordField: 'signup_password',
passReqToCallback: true,
},
async (req, email, password, done) => {
process.nextTick(async () => {
const isTempEmail = await checkEmail(req.body.signup_email);
if (isTempEmail === true) {
return done(null, false, { errors: 'Unable to use this email' });
}
});
}
)
);
每次 isTempEmail
returns undefined
,但如果我注销 checkEmail(email)
末尾返回的是 true
或 false
.
我觉得我在混合使用回调和 async/await 功能,想知道哪里出错了。
目前您的 checkEmail
函数没有 return 任何东西。相反:
return fetch(url)
.then(response => response.json())
.then(async json => {
let result;
if (json.status === 200 && json.temporary === true) {
result = true;
} else {
result = false;
}
return result;
})
.catch(async function(error) {
console.log(`Error whilst checking email ${error}`);
});
这是因为您要return将响应的结果返回给调用者。你不需要 return await
因为那是多余的——async
函数总是 return 一个承诺。如果 return 值不是一个承诺(例如你 return await fetch(url)
),它被包装在一个承诺中。在 returning 到呼叫者之前。所以 return fetch(url)
就足够了。
您甚至可以通过将 .then
调用转换为 await
并将其编写为同步来进一步简化。
我正在使用 Passport JS 本地策略,并想添加一个自定义函数来对用户提供的电子邮件进行一些验证
async function checkEmail(email) {
const url = `https://endpoint-to-url/${email}`;
await fetch(url)
.then(response => response.json())
.then(async json => {
let result;
if (json.status === 200 && json.temporary === true) {
result = true;
} else {
result = false;
}
return result;
})
.catch(async function(error) {
console.log(`Error whilst checking email ${error}`);
});
}
护照 JS
passport.use(
'local-signup',
new LocalStrategy(
{
usernameField: 'signup_email',
passwordField: 'signup_password',
passReqToCallback: true,
},
async (req, email, password, done) => {
process.nextTick(async () => {
const isTempEmail = await checkEmail(req.body.signup_email);
if (isTempEmail === true) {
return done(null, false, { errors: 'Unable to use this email' });
}
});
}
)
);
每次 isTempEmail
returns undefined
,但如果我注销 checkEmail(email)
末尾返回的是 true
或 false
.
我觉得我在混合使用回调和 async/await 功能,想知道哪里出错了。
目前您的 checkEmail
函数没有 return 任何东西。相反:
return fetch(url)
.then(response => response.json())
.then(async json => {
let result;
if (json.status === 200 && json.temporary === true) {
result = true;
} else {
result = false;
}
return result;
})
.catch(async function(error) {
console.log(`Error whilst checking email ${error}`);
});
这是因为您要return将响应的结果返回给调用者。你不需要 return await
因为那是多余的——async
函数总是 return 一个承诺。如果 return 值不是一个承诺(例如你 return await fetch(url)
),它被包装在一个承诺中。在 returning 到呼叫者之前。所以 return fetch(url)
就足够了。
您甚至可以通过将 .then
调用转换为 await
并将其编写为同步来进一步简化。