打字稿多个顺序等待不阻塞

Typescript multiple sequential awaits not blocking

我正在尝试弄清楚如何让多个背靠背等待调用按顺序发生。这是我的设置:

这是问题代码。这是 运行 使用节点 pg 包对 postgresql 的查询。我基于示例代码 https://node-postgres.com/features/transactions#a-pooled-client-with-async-await.

import { Client, Pool } from 'pg';

// An existing client connection can be passed in so that the calling code
// can manage a transaction. If none is provided, a new connection is
// retrieved from the connection pool.
async function query(
  connPool: Pool,
  querystr: string,
  params: (number | string)[],
  client?: Client
): Promise<any[]> {
  let result;
  let conn: Client = client;
  if (conn) {
    result = await conn.query(querystr, params);
  }
  else {
    conn = await connPool.connect();
    result = await conn.query(querystr, params);
    conn.release();
  }
  return result.rows;
}

我正在从另一个文件调用 query() 函数,如下所示:

const results = await query(myPool, 'SELECT * FROM my_table');
doOtherThing(results);

根据我见过的每个源代码示例,我希望它的行为方式如下:

但是,它是按以下顺序执行的:

关于我做错了什么的任何指导?我是否完全被误导了 async/await 的正确使用方式?

我发现了问题。问题中列出的预期行为是正确的。

在语句 const results = await query(myPool, 'SELECT * FROM my_table'); 和实际函数之间是另一个同名函数 query()。这个其他函数作为我在原始 post 中包含的 query() 函数实现的传递。中间的这个函数没有 return 任何东西,这导致了意外的行为。