[go: up one dir, main page]

Skip to content

Commit

Permalink
fix(task.all): fix typing to support arrays of multiple elements of s…
Browse files Browse the repository at this point in the history
…ame type
  • Loading branch information
hrajchert committed Mar 14, 2018
1 parent 7057cf9 commit 0720652
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@acamica/task",
"version": "0.2.0",
"version": "0.2.1",
"description": "Promise replacement made with TypeScript more suitable for functional programming and error handling",
"keywords": [
"async",
Expand Down Expand Up @@ -104,7 +104,7 @@
"ts-node": "^3.0.6",
"tslint": "^5.4.3",
"tslint-config-acamica": "2.0.0",
"typedoc": "^0.9.0",
"typedoc": "0.11.1",
"typescript": "^2.3.4",
"validate-commit-msg": "^2.12.2"
}
Expand Down
12 changes: 6 additions & 6 deletions src/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,25 +59,25 @@ export class Task <T, E> {
static all <T1, T2, T3, T4, E1, E2, E3, E4> (tasks: [Task<T1, E1>, Task<T2, E2>, Task<T3, E3>, Task<T4, E4>]): Task<[T1, T2, T3, T4], E1 | E2 | E3 | E4>;
static all <T1, T2, T3, E1, E2, E3> (tasks: [Task<T1, E1>, Task<T2, E2>, Task<T3, E3>]): Task<[T1, T2, T3], E1 | E2 | E3>;
static all <T1, T2, E1, E2> (tasks: [Task<T1, E1>, Task<T2, E2>]): Task<[T1, T2], E1 | E2>;
static all <T1, E1> (tasks: [Task<T1, E1>]): Task<[T1], E1>;
static all <T, E> (tasks: Task<T, E>[]) {
static all <T, E> (tasks: Array<Task<T, E>>): Task<[T], E>;
static all (tasks: any): any {
// Flag to track if any Task has resolved
let rejected = false;
// Array that we'll fill with the resolved values, in order
const resolvedValues: T[] = [];
const resolvedValues: any[] = [];
// Counter of resolved Tasks (we can't use resolvedValues.length since we add elements through index)
let resolvedQty = 0;

return new Task((outerResolve, outerReject) => {
tasks.forEach((aTask, index) => {
tasks.forEach((aTask: any, index: number) => {
aTask
.fork(err => {
.fork((err: any) => {
// We do only reject if there was no previous rejection
if (!rejected) {
rejected = true;
outerReject(err);
}
}, x => {
}, (x: any) => {
// Shouldn't resolve if another Task has rejected
if (rejected) {
return;
Expand Down
17 changes: 17 additions & 0 deletions test/task.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,23 @@ describe('Task', () => {
);
});

it('should mantain types when given an array of elements of the same type', cb => {
// GIVEN: a resolved Task
const task = Task.resolve(5);

const multipleTasks = [task, task, task, task, task, task, task, task, task, task, task];

// WHEN: we do a Task.all from the previous tasks
const tAll = Task.all(multipleTasks);


// THEN: the resulting Task is resolved with an array of the resolved value
tAll.fork(
jestAssertNever(cb),
assertFork(cb, x => expect(x).toEqual([5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5]))
);
});

it('should work with a single rejected Task', cb => {
// GIVEN: a rejected Task
const task = Task.reject('Buu!');
Expand Down

0 comments on commit 0720652

Please sign in to comment.