Skip to content

Commit

Permalink
Add Future.err() and rename Future.fork to .poll
Browse files Browse the repository at this point in the history
  • Loading branch information
tshemsedinov committed Jun 13, 2019
1 parent 65ec4dc commit b1d877a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 17 deletions.
12 changes: 8 additions & 4 deletions lib/future.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@ class Future {
return new Future(resolve => resolve(value));
}

static err(error) {
return new Future((resolve, reject) => reject(error));
}

chain(fn) {
return new Future((resolve, reject) =>
this.fork(
value => fn(value).fork(resolve, reject),
this.poll(
value => fn(value).poll(resolve, reject),
error => reject(error)
)
);
Expand All @@ -31,13 +35,13 @@ class Future {
);
}

fork(successed, failed) {
poll(successed, failed) {
this.executor(successed, failed);
}

promise() {
return new Promise((resolve, reject) => {
this.fork(value => resolve(value), error => reject(error));
this.poll(value => resolve(value), error => reject(error));
});
}
}
Expand Down
33 changes: 20 additions & 13 deletions test/future.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
const { Future, futurify } = require('..');
const metatests = require('metatests');

metatests.test('Future map/fork', async test => {
metatests.test('Future map/poll', async test => {
Future.of(3)
.map(x => x ** 2)
.fork(value => {
.poll(value => {
test.strictSame(value, 9);
test.end();
});
Expand All @@ -22,7 +22,7 @@ metatests.test('Future resolve', async test => {
setTimeout(() => {
resolve(5);
}, 0);
}).fork(value => {
}).poll(value => {
test.strictSame(value, 5);
test.end();
}, test.mustNotCall());
Expand All @@ -31,7 +31,14 @@ metatests.test('Future resolve', async test => {
metatests.test('Future reject', async test => {
new Future((resolve, reject) => {
reject(new Error('msg'));
}).fork(test.mustNotCall(), error => {
}).poll(test.mustNotCall(), error => {
test.strictSame(error.message, 'msg');
test.end();
});
});

metatests.test('Future error', async test => {
Future.err(new Error('msg')).poll(test.mustNotCall(), error => {
test.strictSame(error.message, 'msg');
test.end();
});
Expand All @@ -52,7 +59,7 @@ metatests.test('Future catch', async test => {
.map(() => {
throw new Error('msg');
})
.fork(test.mustNotCall, error => {
.poll(test.mustNotCall, error => {
test.strictSame(error.message, 'msg');
test.end();
});
Expand All @@ -64,27 +71,27 @@ metatests.test('Future stateless', async test => {
const f3 = f2.map(x => x ** 3);
const f4 = f1.map(x => x * 2);

f1.fork(value => {
f1.poll(value => {
test.strictSame(value, 3);
});

f1.fork(value => {
f1.poll(value => {
test.strictSame(value, 3);
});

f2.fork(value => {
f2.poll(value => {
test.strictSame(value, 4);
});

f2.fork(value => {
f2.poll(value => {
test.strictSame(value, 4);
});

f3.fork(value => {
f3.poll(value => {
test.strictSame(value, 64);
});

f4.fork(value => {
f4.poll(value => {
test.strictSame(value, 6);
});

Expand All @@ -102,7 +109,7 @@ metatests.test('Future futurify success', async test => {

const f2 = futurify(f1);

f2(10, 20).fork(value => {
f2(10, 20).poll(value => {
test.strictSame(value, 30);
test.end();
});
Expand All @@ -119,7 +126,7 @@ metatests.test('Future futurify fail', async test => {

const f2 = futurify(f1);

f2('10', '20').fork(test.mustNotCall, error => {
f2('10', '20').poll(test.mustNotCall, error => {
test.strictSame(error.message, 'Arguments must be numbers');
test.end();
});
Expand Down

0 comments on commit b1d877a

Please sign in to comment.