toPromise

signature: toPromise() : Promise

옵저버블을 프로미스로 바꾸어줍니다.

⚠️ toPromise 는 옵저버블을 리턴하지 않기때문에, pipable 연산자가 아닙니다.

예시

예시 1: 기본적인 프로미스

( jsBin | jsFiddle )

//옵저버블을 리턴
const sample = val => Rx.Observable.of(val).delay(5000);
//옵저버블을 프로미스로 변환
const example = sample('First Example')
  .toPromise()
  //결과: 'First Example'
  .then(result => {
    console.log('From Promise:', result);
  });

예시 2: Promise.all 사용

( jsBin | jsFiddle )

추가 자료

📂 Source Code: https://github.com/ReactiveX/rxjs/blob/master/src/internal/operators/toPromise.ts

Last updated

Was this helpful?