Skip to content

Commit

Permalink
Add Future.error() and rename Future.fork to .fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
tshemsedinov committed Jun 13, 2019
1 parent 65ec4dc commit 2d7a965
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 error(err) {
return new Future((resolve, reject) => reject(err));
}

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

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

promise() {
return new Promise((resolve, reject) => {
this.fork(value => resolve(value), error => reject(error));
this.fetch(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/fetch', async test => {
Future.of(3)
.map(x => x ** 2)
.fork(value => {
.fetch(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 => {
}).fetch(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 => {
}).fetch(test.mustNotCall(), error => {
test.strictSame(error.message, 'msg');
test.end();
});
});

metatests.test('Future error', async test => {
Future.error(new Error('msg')).fetch(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 => {
.fetch(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.fetch(value => {
test.strictSame(value, 3);
});

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

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

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

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

f4.fork(value => {
f4.fetch(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).fetch(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').fetch(test.mustNotCall, error => {
test.strictSame(error.message, 'Arguments must be numbers');
test.end();
});
Expand Down

0 comments on commit 2d7a965

Please sign in to comment.