From 2d7a965f185ab5bc62b540c57e1057fcc85c239c Mon Sep 17 00:00:00 2001 From: tshemsedinov Date: Thu, 13 Jun 2019 15:47:44 +0300 Subject: [PATCH] Add Future.error() and rename Future.fork to .fetch --- lib/future.js | 12 ++++++++---- test/future.js | 33 ++++++++++++++++++++------------- 2 files changed, 28 insertions(+), 17 deletions(-) diff --git a/lib/future.js b/lib/future.js index cec235c4..3e289ff6 100644 --- a/lib/future.js +++ b/lib/future.js @@ -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) ) ); @@ -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)); }); } } diff --git a/test/future.js b/test/future.js index a056772d..71e97e7c 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/fetch', async test => { Future.of(3) .map(x => x ** 2) - .fork(value => { + .fetch(value => { test.strictSame(value, 9); test.end(); }); @@ -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()); @@ -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(); }); @@ -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(); }); @@ -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); }); @@ -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(); }); @@ -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(); });