为什么我需要在 node-postgres 中使用 async/await 两次
Why do I need to use async/await twice in node-postgres
我写的这段代码似乎有效:
database.js
const {Pool} = require('pg');
const pool = new Pool({
connectionString: process.env.DATABASE_URL,
});
module.exports = {
query: (text, params) => pool.query(text, params)
};
auth_facade.js
const database = require('../../utils/database');
module.exports.findPersonByEmail = async function(email) {
const query = 'SELECT * FROM Person WHERE email = ';
const values = [email];
try {
console.log(1);
const {rows} = await database.query(query, values);
console.log(2);
return rows[0];
} catch (err) {
next(err);
}
};
auth_controller.js
const authFacade = require('./auth_facade');
module.exports.signin = async function(req, res, next) {
console.log(0);
var person = await authFacade.findPersonByEmail(req.body.email);
console.log(3);
};
如我所料,它显示 0123。
但是,我不明白为什么 auth_facade::findPersonByEmail
和 auth_controller::signin
都需要 async/await
?
为什么,如果我从 auth_controller::signin
签名中删除异步和其中的 await
,我不再得到 0123,而是 0132? auth_facade
中的 await 不应该被阻止吗?
您当前的代码:
一家人去商场。 (0) 爸爸累了说"Go ahead do some shopping, I'll wait then we'll all go home together." (1) 一会儿女儿说"I don't feel like going into that shop, I'll just hang out here, waiting for you, and then we'll go back to Dad." (2) 妈妈买完东西给女儿说returns, (3) 两人回来接up爸爸和大家一起回家
你的代码没有外层 await
:
一家人去商场。 (0) 爸爸累了说 "Go ahead do some shopping, I'll be here." (1) 稍后女儿说 "I don't feel like going into that shop, I'll just hang out here, waiting for you, and then we'll go back to Dad." 然而,大约在同一时间, (3) 爸爸转身决定回家,因为等待是失败者。 (2) 妈妈买完东西returns给女儿,他们回来发现爸爸开车走了,她们提着一堆购物袋困在商场里。
女儿和爸爸都需要等待,以免留下一个人。
您在 AuthFacade
和 AuthController
两个地方需要 await
的原因是因为您要处理 两个不同的 Promise :
- one in
findPersonByEmail()
(来自数据库查询)
- 一个 来自
findPersonByEmail()
.
已显示:
findPersonByEmail(email: string): Promise<Person>;
//same difference
let person = await authFacade.findPersonByEmail(req.body.email);
//same difference
authFacade.findPersonByEmail(req.body.email).then(person => {
});
如果您想了解更多信息,请阅读:Up and Running with Asynchronous JavaScript。
我写的这段代码似乎有效:
database.js
const {Pool} = require('pg');
const pool = new Pool({
connectionString: process.env.DATABASE_URL,
});
module.exports = {
query: (text, params) => pool.query(text, params)
};
auth_facade.js
const database = require('../../utils/database');
module.exports.findPersonByEmail = async function(email) {
const query = 'SELECT * FROM Person WHERE email = ';
const values = [email];
try {
console.log(1);
const {rows} = await database.query(query, values);
console.log(2);
return rows[0];
} catch (err) {
next(err);
}
};
auth_controller.js
const authFacade = require('./auth_facade');
module.exports.signin = async function(req, res, next) {
console.log(0);
var person = await authFacade.findPersonByEmail(req.body.email);
console.log(3);
};
如我所料,它显示 0123。
但是,我不明白为什么 auth_facade::findPersonByEmail
和 auth_controller::signin
都需要 async/await
?
为什么,如果我从 auth_controller::signin
签名中删除异步和其中的 await
,我不再得到 0123,而是 0132? auth_facade
中的 await 不应该被阻止吗?
您当前的代码:
一家人去商场。 (0) 爸爸累了说"Go ahead do some shopping, I'll wait then we'll all go home together." (1) 一会儿女儿说"I don't feel like going into that shop, I'll just hang out here, waiting for you, and then we'll go back to Dad." (2) 妈妈买完东西给女儿说returns, (3) 两人回来接up爸爸和大家一起回家
你的代码没有外层 await
:
一家人去商场。 (0) 爸爸累了说 "Go ahead do some shopping, I'll be here." (1) 稍后女儿说 "I don't feel like going into that shop, I'll just hang out here, waiting for you, and then we'll go back to Dad." 然而,大约在同一时间, (3) 爸爸转身决定回家,因为等待是失败者。 (2) 妈妈买完东西returns给女儿,他们回来发现爸爸开车走了,她们提着一堆购物袋困在商场里。
女儿和爸爸都需要等待,以免留下一个人。
您在 AuthFacade
和 AuthController
两个地方需要 await
的原因是因为您要处理 两个不同的 Promise :
- one in
findPersonByEmail()
(来自数据库查询) - 一个 来自
findPersonByEmail()
.
已显示:
findPersonByEmail(email: string): Promise<Person>;
//same difference
let person = await authFacade.findPersonByEmail(req.body.email);
//same difference
authFacade.findPersonByEmail(req.body.email).then(person => {
});
如果您想了解更多信息,请阅读:Up and Running with Asynchronous JavaScript。