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

Commit ae70e1e

Browse files
committed
Added promise() method
1 parent 86b74b1 commit ae70e1e

File tree

4 files changed

+38
-2
lines changed

4 files changed

+38
-2
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ Output:
181181
- [`ap`](#ap)
182182
- [`chain`](#chain)
183183
- [`bimap`](#bimap)
184+
- [`future`](#future)
184185

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

314+
### <a id="ap"></a> `future()`
315+
316+
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.
317+
318+
Returns: **FutureInstance**
319+
- - -
320+
313321
## Consuming / Collapsing Methods
314322

315323
### <a id="fork"></a> `fork(left)(right)`

index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ const { Reader } = Monet
8080
* fork: (left: any) => (right: any) => F.Cancel;
8181
* collapse: () => FutureInstanceT<T>;
8282
* promise: () => Promise<any>;
83+
* future: () => FutureInstanceT<T>;
8384
* toString: () => string;
8485
* first: () => Promise<any>;
8586
* 'fantasy-land/map': (pred: ResultPredicate<T, T>) => IodioInstance<T>;
@@ -170,6 +171,10 @@ const Iodio = (pPred, qbR, fR) => {
170171

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

174+
const future = () => F.Future(
175+
(reject, resolve) => collapse().pipe(F.fork(reject)(resolve))
176+
)
177+
173178
return {
174179
pMap,
175180
qMap,
@@ -183,6 +188,7 @@ const Iodio = (pPred, qbR, fR) => {
183188
toString,
184189
first,
185190
collapse,
191+
future,
186192
// Fantay Land Interface
187193
'fantasy-land/map': map,
188194
'fantasy-land/ap': ap,

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "iodio",
3-
"version": "0.1.1",
3+
"version": "0.1.2",
44
"type": "module",
55
"description": "Monadic query builder (Knex wrapper: Fluture powered)",
66
"main": "index.js",

tests/core.test.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ test('Bifunctor: bimap', async t => {
242242
})
243243

244244
test('Bifunctor: identity', async t => {
245-
const { u, g1, f1 } = t.context
245+
const { u } = t.context
246246
t.deepEqual(await u.bimap(I, I).first(), await u.first())
247247
})
248248

@@ -268,3 +268,25 @@ test('Bifunctor: composition', async t => {
268268
.first()
269269
)
270270
})
271+
272+
test('to future', async t => {
273+
const { db } = t.context
274+
let lazyGuard = true
275+
const query = Iodio.lift(db, ['Track'])
276+
.qMap(qb => {
277+
lazyGuard = false
278+
return qb.where({ Composer: 'Nirvana' })
279+
})
280+
.qMap(qb => qb.limit(2))
281+
.map(rows => {
282+
lazyGuard = false
283+
return rows.map(track => track.Name)
284+
})
285+
286+
t.is(lazyGuard, true)
287+
const futureValue = await query.future()
288+
t.is(lazyGuard, true)
289+
const value = await F.promise(futureValue)
290+
t.is(lazyGuard, false)
291+
t.deepEqual(value, ['Aneurysm', 'Smells Like Teen Spirit'])
292+
})

0 commit comments

Comments
 (0)