Skip to content

Commit

Permalink
feat(task): simplified Task.all implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
alexsasharegan committed Sep 1, 2019
1 parent c8a07a6 commit 3ea0b13
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions src/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -408,17 +408,18 @@ export class Task<OkType, ErrType> {
tasks: Task<OkType, ErrType>[]
): Task<OkType[], ErrType> {
return new Task<OkType[], ErrType>(({ Ok, Err }) => {
const matcher = {
Ok: (okValue: OkType) => Promise.resolve(okValue),
Err: (errValue: ErrType) => Promise.reject(errValue),
};
const do_match: (r: Result<OkType, ErrType>) => Promise<OkType> = r =>
r.match(matcher);
const run_task = (t: Task<OkType, ErrType>) => t.run().then(do_match);

Promise.all(tasks.map(run_task))
Promise.all(tasks.map(task_all_executor))
.then(Ok)
.catch(Err);

function task_all_executor(t: Task<OkType, ErrType>) {
return new Promise<OkType>((resolve, reject) =>
t.executor({
Ok: resolve,
Err: reject,
})
);
}
});
}

Expand Down

0 comments on commit 3ea0b13

Please sign in to comment.