NestJs 异步控制器方法与在方法内调用 await / async
NestJs Async controller method vs calling await / async inside a method
我对 NodeJs 和 NestJs 有点陌生。我一直想知道在控制器中使用异步作为方法 return 类型与在常规方法中执行异步操作有什么区别?如果这个 API 上有巨大的流量(例如 40K req/min),NodeJs 如何处理这两种情况下的请求。它会在第二个示例中阻塞而在第一个示例中不阻塞还是会以类似的方式工作?
例如:
@Controller('cats')
export class CatsController {
constructor(private catsService: CatsService) {}
@Post()
async sample() {
return "1234";
}
}
对
@Controller('cats')
export class CatsController {
constructor(private catsService: CatsService) {}
@Post()
function sample() {
return await methodX();
}
async function methodX(){
return "1234"
}
请忽略 sample() 和 methodX() 中的内容仅供示例。
首先,将方法标记为 async
使您可以在其中使用 await
关键字。
例如在您提供的第二个示例中,您需要将方法 sample
标记为 async
到 await
methodX
:
@Post()
async function sample() {
// Now `await` can be used inside `sample`
return await methodX();
}
所以回答你的问题
what's the difference between using async as the method return type inside a controller vs executing async operation inside a regular method ?
有none。在这两种情况下,控制器的方法都需要标记为 async
。在方法主体中执行路由应该执行的操作或在另一个 async
方法中提取它只是代码组织的问题。
Will it be blocking in the 2nd example and non blocking in the 1st or would it work in a similar way?
两个示例的工作方式相似。
其次,如果在其主体中没有执行真正的异步操作(如对其他服务或 setTimeout
的请求),将方法标记为 async
实际上并不会使其异步。意思是下面的samples
// Sample 1
@Post()
async sample() {
return "1234";
}
// Sample 2
@Post()
function async sample() {
return await methodX();
}
async function methodX(){
return "1234"
}
都相当于同步方式
@Post()
syncSample() {
return "1234";
}
最后,正如@Micael Levi 所说,return await
在句法上是正确的,但应该避免使用。考虑这个示例:
@Post()
function async sample() {
return await methodX();
}
async function methodX(){
throw new Error('something failed')
}
因为方法 sample
return await
methodX
, sample
不会出现在堆栈跟踪中,这使得调试更加困难。
我们更喜欢这个示例:
@Post()
function async sample() {
const result = await methodX();
return result;
}
async function methodX(){
throw new Error('something failed')
}
我对 NodeJs 和 NestJs 有点陌生。我一直想知道在控制器中使用异步作为方法 return 类型与在常规方法中执行异步操作有什么区别?如果这个 API 上有巨大的流量(例如 40K req/min),NodeJs 如何处理这两种情况下的请求。它会在第二个示例中阻塞而在第一个示例中不阻塞还是会以类似的方式工作?
例如:
@Controller('cats')
export class CatsController {
constructor(private catsService: CatsService) {}
@Post()
async sample() {
return "1234";
}
}
对
@Controller('cats')
export class CatsController {
constructor(private catsService: CatsService) {}
@Post()
function sample() {
return await methodX();
}
async function methodX(){
return "1234"
}
请忽略 sample() 和 methodX() 中的内容仅供示例。
首先,将方法标记为 async
使您可以在其中使用 await
关键字。
例如在您提供的第二个示例中,您需要将方法 sample
标记为 async
到 await
methodX
:
@Post()
async function sample() {
// Now `await` can be used inside `sample`
return await methodX();
}
所以回答你的问题
what's the difference between using async as the method return type inside a controller vs executing async operation inside a regular method ?
有none。在这两种情况下,控制器的方法都需要标记为 async
。在方法主体中执行路由应该执行的操作或在另一个 async
方法中提取它只是代码组织的问题。
Will it be blocking in the 2nd example and non blocking in the 1st or would it work in a similar way?
两个示例的工作方式相似。
其次,如果在其主体中没有执行真正的异步操作(如对其他服务或 setTimeout
的请求),将方法标记为 async
实际上并不会使其异步。意思是下面的samples
// Sample 1
@Post()
async sample() {
return "1234";
}
// Sample 2
@Post()
function async sample() {
return await methodX();
}
async function methodX(){
return "1234"
}
都相当于同步方式
@Post()
syncSample() {
return "1234";
}
最后,正如@Micael Levi 所说,return await
在句法上是正确的,但应该避免使用。考虑这个示例:
@Post()
function async sample() {
return await methodX();
}
async function methodX(){
throw new Error('something failed')
}
因为方法 sample
return await
methodX
, sample
不会出现在堆栈跟踪中,这使得调试更加困难。
我们更喜欢这个示例:
@Post()
function async sample() {
const result = await methodX();
return result;
}
async function methodX(){
throw new Error('something failed')
}