为什么我需要在node-postgres中使用async/await两次 [英] Why do I need to use async/await twice in node-postgres

查看:227
本文介绍了为什么我需要在node-postgres中使用async/await两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了似乎有效的代码:

I wrote this code that seems to be working:

database.js

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

auth_facade.js

const database = require('../../utils/database');

module.exports.findPersonByEmail = async function(email) {
const query = 'SELECT * FROM Person WHERE email = $1';
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

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.

It shows, as I expected, 0123.

但是,我不明白为什么在auth_facade::findPersonByEmailauth_controller::signin上都需要async/await?

However, I don't understand why I need the async/await on both auth_facade::findPersonByEmail and auth_controller::signin?

为什么,如果我从auth_controller::signin签名和其中的await中删除了异步,我将不再得到0123,而是得到0132?还是应该在auth_facade中阻止它被阻塞?

Why, if I remove the async from auth_controller::signin signature and the await inside it, I don't get 0123 anymore, but 0132 instead? Shouldn't it be blocked anyway be the await in auth_facade?

推荐答案

您当前的代码:

一家人去购物中心. (0)爸爸累了,说:继续购物,我等着我们一起回家." (1)后来的一个女儿说:我不想去那家商店,我会在这里闲逛,等着你,然后我们再回去给爸爸." (2)妈妈完成购物并返回女儿,(3)他们俩都回来接爸爸,一起回家.

A family goes to a mall. (0) Dad is tired and says "Go ahead do some shopping, I'll wait then we'll all go home together." (1) A little later daughter says "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) Mom finishes shopping and returns to daughter, (3) they both come back to pick up Dad and all go home together.

您的代码没有外部await:

一家人去购物中心. (0)爸爸累了,说:继续购物,我会在这里." (1)后来的一个女儿说:我不想去那家商店,我会在这里闲逛,等着你,然后我们再回去给爸爸."但是,大约在同一时间,(3)爸爸转身并决定回家,因为等待是在等待失败者. (2)妈妈结束购物并返回女儿,他们俩都回来找爸爸开车走了,他们带着一堆购物袋被困在商场里.

A family goes to a mall. (0) Dad is tired and says "Go ahead do some shopping, I'll be here." (1) A little later daughter says "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." However, at about the same time, (3) Dad turns around and decides to go home, because waiting is for losers. (2) Mom finishes shopping and returns to daughter, they both come back to find Dad is gone with the car and they're stuck in the mall with a bunch of shopping bags.

女儿和爸爸都需要等待,以免他们离开别人.

Both daughter and Dad need to wait in order for them not to leave someone behind.

这篇关于为什么我需要在node-postgres中使用async/await两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆