soon530 / nestjs-study

來學一下nestjs吧!
0 stars 0 forks source link

處理回應的方式 #10

Open soon530 opened 2 years ago

soon530 commented 2 years ago

有很多方式,最推的就是retrun就好了。

標準模式

import { Controller, Get } from '@nestjs/common';

@Controller('todos')
export class TodoController {
  @Get()
  getAll() {
    return [];
  }
}

非同步 為什麼要這樣?

import { Controller, Get } from '@nestjs/common';

@Controller('todos')
export class TodoController {
  @Get()
  async getAll() {
    return new Promise((resolve, reject) => setTimeout(() => resolve([]), 1000));
  }
}

RxJS 沒用過

import { Controller, Get } from '@nestjs/common';
import { of } from 'rxjs';

@Controller('todos')
export class TodoController {
  @Get()
  getAll() {
    return of([]);
  }
}