입력:22/12/12수정:24/01/05
fp-ts sequence 연습
- 사실상 동시실행급
type Model = { name: string }
const data: Model[] = [{name: 'a'}, {name: 'b'}, {name: 'c'}, {name: 'd'}, {name: 'e'}]
pipe(
data,
A.traverse(TE.ApplicativeSeq)((entity: Model) => {
const sleep = new Promise(async () => {
console.log(`${entity.name} 시작`)
await new Promise(resolve => setTimeout(resolve, Math.floor(Math.random() * 3000)))
console.log(`${entity.name} 끝`)
})
return pipe(
TE.tryCatch(() => sleep, E.toError),
)
})
)()
- 하나씩 호출하는데 서옹
type Model = { name: string }
const data: Model[] = [{name: 'a'}, {name: 'b'}, {name: 'c'}, {name: 'd'}, {name: 'e'}]
pipe(
data,
A.map(it => TE.tryCatch(() => logJob(it), E.toError)),
//TE.ApplicativePar로 변경시 동시호출
A.sequence(TE.ApplicativeSeq),
)()
- 단서1
TE.chain((data) => {
console.log(data)
return TE.of(data)
}),
/* TE Chain은 원형을 받고 TaskEither를 리턴해줌*/
TE.chain((data) => {
console.log(data)
return TE.of(data)
}),
최종성공
pipe(
this.useCase.cronPortJob(cronKey),
TE.chain((data) =>
pipe(
data,
A.map((it: InvestorPortDTO) =>
this.cronPortDetailJob(it.investorCik, it.fillingId)),
A.map(delay(1000)),
A.sequence(TE.ApplicativeSeq),
),
),
)