diff --git a/lib/future.js b/lib/future.js index cec235c4..2e563531 100644 --- a/lib/future.js +++ b/lib/future.js @@ -9,12 +9,13 @@ 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), - error => reject(error) - ) + this.run(value => fn(value).run(resolve, reject), error => reject(error)) ); } @@ -31,13 +32,13 @@ class Future { ); } - fork(successed, failed) { + run(successed, failed) { this.executor(successed, failed); } promise() { return new Promise((resolve, reject) => { - this.fork(value => resolve(value), error => reject(error)); + this.run(value => resolve(value), error => reject(error)); }); } } diff --git a/test/future.js b/test/future.js index a056772d..59e2ea9a 100644 --- a/test/future.js +++ b/test/future.js @@ -3,10 +3,10 @@ const { Future, futurify } = require('..'); const metatests = require('metatests'); -metatests.test('Future map/fork', async test => { +metatests.test('Future map/run', async test => { Future.of(3) .map(x => x ** 2) - .fork(value => { + .run(value => { test.strictSame(value, 9); test.end(); }); @@ -22,7 +22,7 @@ metatests.test('Future resolve', async test => { setTimeout(() => { resolve(5); }, 0); - }).fork(value => { + }).run(value => { test.strictSame(value, 5); test.end(); }, test.mustNotCall()); @@ -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 => { + }).run(test.mustNotCall(), error => { + test.strictSame(error.message, 'msg'); + test.end(); + }); +}); + +metatests.test('Future error', async test => { + Future.err(new Error('msg')).run(test.mustNotCall(), error => { test.strictSame(error.message, 'msg'); test.end(); }); @@ -52,7 +59,7 @@ metatests.test('Future catch', async test => { .map(() => { throw new Error('msg'); }) - .fork(test.mustNotCall, error => { + .run(test.mustNotCall, error => { test.strictSame(error.message, 'msg'); test.end(); }); @@ -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.run(value => { test.strictSame(value, 3); }); - f1.fork(value => { + f1.run(value => { test.strictSame(value, 3); }); - f2.fork(value => { + f2.run(value => { test.strictSame(value, 4); }); - f2.fork(value => { + f2.run(value => { test.strictSame(value, 4); }); - f3.fork(value => { + f3.run(value => { test.strictSame(value, 64); }); - f4.fork(value => { + f4.run(value => { test.strictSame(value, 6); }); @@ -102,7 +109,7 @@ metatests.test('Future futurify success', async test => { const f2 = futurify(f1); - f2(10, 20).fork(value => { + f2(10, 20).run(value => { test.strictSame(value, 30); test.end(); }); @@ -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').run(test.mustNotCall, error => { test.strictSame(error.message, 'Arguments must be numbers'); test.end(); });