Skip to content

Commit

Permalink
feat(future): add zip
Browse files Browse the repository at this point in the history
  • Loading branch information
saint1991 committed Apr 20, 2018
1 parent ace58d8 commit 5314b36
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/future/future.ts
Expand Up @@ -171,6 +171,10 @@ class Future<A> {
);
}

zip<B>(that: Future<B>): Future<[A, B]> {
return this.flatMap((value: A) => that.map((thatValue: B) => [value, thatValue] as [A, B]));
}

}

export { Future };
11 changes: 11 additions & 0 deletions test/future/future.ts
Expand Up @@ -195,3 +195,14 @@ test('Future#recoverWith', async(t: ExecutionContext) => {

await t.throws(failure.recoverWith((e: Error): Future<string> => { throw error; }).promise);
});

test('Future#zip', async(t: ExecutionContext) => {
t.plan(3);

const value: Future<number> = Future.create(10);
const successfulZip: [string, number] = await success.zip(value).promise;
t.deepEqual(successfulZip, ['hello', 10]);

await t.throws(failure.zip(value).promise);
await t.throws(value.zip(failure).promise);
});

0 comments on commit 5314b36

Please sign in to comment.