침착맨의 미라클 방송을 본받아 미라클 블로그 글쓰기를 해볼까 한다.

미라클 글쓰기이므로 부담 가지 않게, 두서 없더라도, 짧은 내용을 휘갈기듯 써볼 생각임.
Typescript에는 PromiseLike라는 interface가 있다.
interface PromiseLike<T> {
/**
* Attaches callbacks for the resolution and/or rejection of the Promise.
* @param onfulfilled The callback to execute when the Promise is resolved.
* @param onrejected The callback to execute when the Promise is rejected.
* @returns A Promise for the completion of which ever callback is executed.
*/
then<TResult1 = T, TResult2 = never>(
onfulfilled?:
((value: T) => TResult1 | PromiseLike<TResult1>) |
undefined |
null,
onrejected?:
((reason: any) => TResult2 | PromiseLike<TResult2>) |
undefined |
null
): PromiseLike<TResult1 | TResult2>;
}
Promise와의 차이점은 catch 와 finally 가 존재하지 않는다는 것이다.
interface Promise<T> {
/**
* Attaches callbacks for the resolution and/or rejection of the Promise.
* @param onfulfilled The callback to execute when the Promise is resolved.
* @param onrejected The callback to execute when the Promise is rejected.
* @returns A Promise for the completion of which ever callback is executed.
*/
then<TResult1 = T, TResult2 = never>(
onfulfilled?:
((value: T) => TResult1 | PromiseLike<TResult1>) |
undefined |
null,
onrejected?:
((reason: any) => TResult2 | PromiseLike<TResult2>) |
undefined |
null
): Promise<TResult1 | TResult2>;
/**
* Attaches a callback for only the rejection of the Promise.
* @param onrejected The callback to execute when the Promise is rejected.
* @returns A Promise for the completion of the callback.
*/
catch<TResult = never>(
onrejected?:
((reason: any) => TResult | PromiseLike<TResult>) |
undefined |
null
): Promise<T | TResult>;
}
interface Promise<T> {
/**
* Attaches a callback that is invoked when the Promise is settled (fulfilled or rejected). The
* resolved value cannot be modified from the callback.
* @param onfinally The callback to execute when the Promise is settled (fulfilled or rejected).
* @returns A Promise for the completion of the callback.
*/
finally(onfinally?: (() => void) | undefined | null): Promise<T>;
}
PromiseLike가 처음 등장한 시점은 2015년 Typescript 1.6.0-beta 부터이며, 개념적으로 Thenable과 유사하다.
다음 문제들을 해결하는데 사용된다.
– Bluebird 나 Deffered 등, 비표준 Promise interface 와의 호환성
– Observable 같은 다양한 시나리오를 지닌 비동기 객체의 Promise 변환
특히 error 처리 시나리오의 경우, Promise는 catch 에 진입하면 회복 불가능한 반면
Observable은 catchError, retry, retryWhen 등 다양한 시나리오가 존재한다.
이는 표준 Promise interface로는 처리하기 까다로운 면이 있기에
PromiseLike 같은 최소한의 Thenable 인터페이스로 어설픈 변환을 하는 편이 낫다.




댓글 남기기