From 909f9f41451b0114096f06fe154e2c3d4b61e493 Mon Sep 17 00:00:00 2001 From: Ian Hofmann-Hicks Date: Sat, 18 Feb 2017 11:23:48 -0800 Subject: [PATCH] add Resolved to Async and move rejected to Rejected --- README.md | 4 ++-- crocks/Async.js | 8 ++++++-- crocks/Async.spec.js | 36 +++++++++++++++++++++++++++--------- transforms/eitherToAsync.js | 2 +- transforms/maybeToAsync.js | 4 ++-- 5 files changed, 38 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index bfb78146a..cad27257d 100644 --- a/README.md +++ b/README.md @@ -73,7 +73,7 @@ All `Crocks` are Constructor functions of the given type, with `Writer` being an | Crock | Constructor | Instance | |---|:---|:---| | `Arrow` | `empty` | `both`, `concat`, `contramap`, `empty`, `first`, `map`, `promap`, `runWith`, `second`, `value` | -| `Async` | `all`, `fromNode`, `fromPromise`, `of`, `rejected` | `ap`, `bimap`, `chain`, `coalesce`, `fork`, `map`, `of`, `swap`, `toPromise` | +| `Async` | , `Rejected`, `Resolved`, `all`, `fromNode`, `fromPromise`, `of` | `ap`, `bimap`, `chain`, `coalesce`, `fork`, `map`, `of`, `swap`, `toPromise` | | `Const` | -- | `ap`, `chain`, `concat`, `equals`, `map`, `value` | | `Either` | `Left`, `Right`, `of`| `ap`, `bimap`, `chain`, `coalesce`, `either`, `equals`, `map`, `of`, `sequence`, `swap`, `traverse` | | `Identity` | `of` | `ap`, `chain`, `equals`, `map`, `of`, `sequence`, `traverse`, `value` | @@ -412,7 +412,7 @@ const anotherInc = // resolveValue : a -> Async _ a const resolveValue = - Async.of + Async.Resolved resolveValue(3) // Resolved 3 .chain(asyncInc) // Resolved 4 diff --git a/crocks/Async.js b/crocks/Async.js index e359599a1..0f6358b56 100644 --- a/crocks/Async.js +++ b/crocks/Async.js @@ -27,7 +27,7 @@ const _type = const _of = x => Async((_, resolve) => resolve(x)) -const _rejected = +const Rejected = x => Async((reject, _) => reject(x)) function all(asyncs) { @@ -221,9 +221,13 @@ function Async(fn) { Async.type = _type Async.of = _of -Async.rejected = _rejected + +Async.Rejected = Rejected +Async.Resolved = _of + Async.fromPromise = fromPromise Async.fromNode = fromNode + Async.all = all module.exports = Async diff --git a/crocks/Async.spec.js b/crocks/Async.spec.js index 65d09a159..1b2ce9083 100644 --- a/crocks/Async.spec.js +++ b/crocks/Async.spec.js @@ -24,7 +24,10 @@ test('Async', t => { t.ok(isFunction(Async.of), 'provides an of function') t.ok(isFunction(Async.type), 'provides a type function') - t.ok(isFunction(Async.rejected), 'provides a rejected function') + + t.ok(isFunction(Async.Resolved), 'provides a Resolved function') + t.ok(isFunction(Async.Rejected), 'provides a Rejected function') + t.ok(isFunction(Async.fromPromise), 'provides a fromPromise function') t.throws(Async, TypeError, 'throws with no parameters') @@ -42,9 +45,9 @@ test('Async', t => { t.end() }) -test('Async rejected', t => { +test('Async Rejected', t => { const x = 'sorry' - const m = Async.rejected(x) + const m = Async.Rejected(x) const rej = sinon.spy() const res = sinon.spy() @@ -57,6 +60,21 @@ test('Async rejected', t => { t.end() }) +test('Async Resolved', t => { + const x = 'sorry' + const m = Async.Resolved(x) + + const rej = sinon.spy() + const res = sinon.spy() + + m.fork(rej, res) + + t.ok(res.calledWith(x), 'calls the resolved function with resolved value') + t.notOk(rej.called, 'does not call the rejected function') + + t.end() +}) + test('Async fromPromise', t => { const resProm = x => new Promise((res, _) => res(x)) @@ -186,8 +204,8 @@ test('Async all resolution', t => { const res = y => x => t.same(x, y, 'resolves with array of values when all resolve') const empty = y => x => t.same(x, y, 'resolves with an empty array when passed an empty array') - Async.all([ Async.of(val), Async.of(val) ]).fork(rej(bad), res([ val, val ])) - Async.all([ Async.rejected(bad), Async.of(val) ]).fork(rej(bad), res([ val, val ])) + Async.all([ Async.Resolved(val), Async.Resolved(val) ]).fork(rej(bad), res([ val, val ])) + Async.all([ Async.Rejected(bad), Async.Resolved(val) ]).fork(rej(bad), res([ val, val ])) Async.all([]).fork(rej(bad), empty([])) t.end() @@ -256,8 +274,8 @@ test('Async toPromise', t => { const rej = y => x => t.equal(x, y, 'rejects a rejected Async') const res = y => x => t.equal(x, y, 'resolves a resolved Async') - Async.rejected(val).toPromise().then(res(val)).catch(rej(val)) - Async.of(val).toPromise().then(res(val)).catch(rej(val)) + Async.Rejected(val).toPromise().then(res(val)).catch(rej(val)) + Async.Resolved(val).toPromise().then(res(val)).catch(rej(val)) t.plan(2) }) @@ -454,8 +472,8 @@ test('Async bimap functionality', t => { test('Async bimap properties (Bifunctor)', t => { const bimap = (f, g) => m => m.bimap(f, g) - const rej = Async.rejected(10) - const res = Async.of(10) + const rej = Async.Rejected(20) + const res = Async.Resolved(10) const rejId = sinon.spy() const resId = sinon.spy() diff --git a/transforms/eitherToAsync.js b/transforms/eitherToAsync.js index 9fb3a11c8..ed5feda63 100644 --- a/transforms/eitherToAsync.js +++ b/transforms/eitherToAsync.js @@ -10,7 +10,7 @@ const Either = require('../crocks/Either') const Async = require('../crocks/Async') const applyTransform = either => - either.either(Async.rejected, Async.of) + either.either(Async.Rejected, Async.Resolved) // eitherToAsync : Either e a -> Async e a // eitherToAsync : (a -> Either e b) -> a -> Async e b diff --git a/transforms/maybeToAsync.js b/transforms/maybeToAsync.js index 4d8333703..f12741fe8 100644 --- a/transforms/maybeToAsync.js +++ b/transforms/maybeToAsync.js @@ -12,8 +12,8 @@ const Async = require('../crocks/Async') const applyTransform = (left, maybe) => maybe.either( - constant(Async.rejected(left)), - Async.of + constant(Async.Rejected(left)), + Async.Resolved ) // maybeToAsync : e -> Maybe a -> Async e a