글

라벨이 catch인 게시물 표시

TypeScript_JEST TypeScript로 Node TestCast만들기 : 에러상황을 테스트하기 -3

이미지
  // src/app/Main.ts import { parse , UrlWithParsedQuery } from "url" ; export class Fun { public static parseUrl ( url : string ) : UrlWithParsedQuery { if (! url || url . length <= 0 ) throw Error ( 'url is empty' ) // url이 비어있을시 에러발생 return parse ( url , true ); } public static toUpperC ( arg : string ){ return arg . toUpperCase (); } } // src/test/Main.test.ts import { Fun } from '../app/Main' describe ( 'Main test suite' , () => { // 해당 describe테스트에서 아래만 제외한다. test ( 'first test' , () => { console . log ( 'test work!!!' ) const str = Fun . toUpperC ( 'str' ); expect ( str ). toBe ( 'STR' ); // Fun.toUpper의 결과가 문자열 STR 이어야 한다 }); test ( 'parse URL' , () => { const url : string = 'http://localhost:3000/signup' ; const port : string = url . split ( 'localhost:...

Promise 개념

이미지
const  promise  =   new   Promise ( ( resolve ,   reject )   =>   {      console . log ( ' Execute Promise ' ) ;      setTimeout ( ()   =>   {          resolve ( ' Alex ' ) ;          // reject(new Error('network failed'));      },   2000 ) ; } ) ; promise . then ( ( value )   =>   {      console . log ( value ) ; } ) . catch (error  =>   {      console . log ( error ) ; } ) . finally ( ()   =>   {      console . log ( ` Promise finish ` ) } )  Promise를 선언할때 선언과 동시에 내부가 동작되는것을 알수 있다. 만약 선언후 나중에 통신을 할려고 하면 그때 선언하는 것이 리소스 관리상 유리하다. .then : resolve가 실행 될시 전달인자(value)를 반환한다. .catch : reject가 실행 될시 전달인자(error)를 반환한다.  -> 만약 .catch가 없이 reject가 실행시 아래와 같이 반환한다. .fina...

Promise 예제

이미지
const  stage1_Error  =   false ; const  stage2_Error  =   false ; const  stage3_Error  =   false ; function   stage1 (){      return   new   Promise ( ( resolve ,   reject )   =>   {          setTimeout ( ()   =>   {              if ( stage1_Error   ===   false )  resolve ( ' stage1 clear ' ) ;              else   reject ( new   Error ( ` stage1 Error ` )) ;          },   1000 ) ;      } ) ; } function   stage2 ( ele ){      return   new   Promise ( ( resolve ,   reject )   =>   {          setTimeout ( ()   =>   { ...