Skip to content
This repository has been archived by the owner on Feb 22, 2021. It is now read-only.

Commit

Permalink
Added promise() method
Browse files Browse the repository at this point in the history
  • Loading branch information
FbN committed Oct 21, 2020
1 parent 86b74b1 commit ae70e1e
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ Output:
- [`ap`](#ap)
- [`chain`](#chain)
- [`bimap`](#bimap)
- [`future`](#future)

### Consuming / Collapsing Methods
- [`fork`](#fork)
Expand Down Expand Up @@ -310,6 +311,13 @@ Call the function wrapped as future value in the supplied IodioInstance passing
Returns: **IodioInstance**
- - -

### <a id="ap"></a> `future()`

Transform Iodio to a Fluture Future wich wrapped value equals query result plus computation. No query is executed calling this method, query and computation will be done at time of forking returned future.

Returns: **FutureInstance**
- - -

## Consuming / Collapsing Methods

### <a id="fork"></a> `fork(left)(right)`
Expand Down
6 changes: 6 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ const { Reader } = Monet
* fork: (left: any) => (right: any) => F.Cancel;
* collapse: () => FutureInstanceT<T>;
* promise: () => Promise<any>;
* future: () => FutureInstanceT<T>;
* toString: () => string;
* first: () => Promise<any>;
* 'fantasy-land/map': (pred: ResultPredicate<T, T>) => IodioInstance<T>;
Expand Down Expand Up @@ -170,6 +171,10 @@ const Iodio = (pPred, qbR, fR) => {

const first = () => promise().then(r => r && (Array.isArray(r) ? r[0] : r))

const future = () => F.Future(
(reject, resolve) => collapse().pipe(F.fork(reject)(resolve))
)

return {
pMap,
qMap,
Expand All @@ -183,6 +188,7 @@ const Iodio = (pPred, qbR, fR) => {
toString,
first,
collapse,
future,
// Fantay Land Interface
'fantasy-land/map': map,
'fantasy-land/ap': ap,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "iodio",
"version": "0.1.1",
"version": "0.1.2",
"type": "module",
"description": "Monadic query builder (Knex wrapper: Fluture powered)",
"main": "index.js",
Expand Down
24 changes: 23 additions & 1 deletion tests/core.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ test('Bifunctor: bimap', async t => {
})

test('Bifunctor: identity', async t => {
const { u, g1, f1 } = t.context
const { u } = t.context
t.deepEqual(await u.bimap(I, I).first(), await u.first())
})

Expand All @@ -268,3 +268,25 @@ test('Bifunctor: composition', async t => {
.first()
)
})

test('to future', async t => {
const { db } = t.context
let lazyGuard = true
const query = Iodio.lift(db, ['Track'])
.qMap(qb => {
lazyGuard = false
return qb.where({ Composer: 'Nirvana' })
})
.qMap(qb => qb.limit(2))
.map(rows => {
lazyGuard = false
return rows.map(track => track.Name)
})

t.is(lazyGuard, true)
const futureValue = await query.future()
t.is(lazyGuard, true)
const value = await F.promise(futureValue)
t.is(lazyGuard, false)
t.deepEqual(value, ['Aneurysm', 'Smells Like Teen Spirit'])
})

0 comments on commit ae70e1e

Please sign in to comment.