Skip to content

Instantly share code, notes, and snippets.

@Restuta
Last active May 18, 2017 00:44
Show Gist options
  • Save Restuta/a836d8dcf7d1442645da1103ca330d1f to your computer and use it in GitHub Desktop.
Save Restuta/a836d8dcf7d1442645da1103ca330d1f to your computer and use it in GitHub Desktop.
P.coroutine and _.curry issue
function asyncSum(x, y) {
return new Promise(resolve => setInterval(
() => resolve(x + y)), 10
)
}
function asyncSquare(x, y) {
return new Promise(resolve => setInterval(
() => resolve(x * y)), 10
)
}
// WORKS
test('can I haz vanilla curry?', async t => {
const _ = require('_')
const asyncSumAndSquare = function asyncSumAndSquare(x, y) {
return asyncSum(x, y).then(sum => asyncSquare(sum, sum))
}
const parialAsyncSumAndSquare = _.curry(asyncSumAndSquare)
const result = await parialAsyncSumAndSquare(2)(2)
t.is(result, 16)
})
// FAILS, since P.coroutine messes up with parameters, it returns a function that accepts ONE argument
// co behaves in the same way, tested with lodash 3 and 4
test('bad curry', async t => {
const _ = require('_')
const P = require('bluebird')
const asyncSumAndSquare = P.coroutine(function *asyncSumAndSquare(x, y) {
const sum = yield asyncSum(x, y)
const square = yield asyncSquare(sum, sum)
return square
})
// curry is unable to curry P.coroutine
const parialAsyncSumAndSquare = _.curry(asyncSumAndSquare)
const result = await parialAsyncSumAndSquare(2)(2)
t.is(result, 16)
})
// WORKS
test('manual curry', async t => {
const P = require('bluebird')
const asyncSumAndSquare = P.coroutine(function *asyncSumAndSquare(x, y) {
const sum = yield asyncSum(x, y)
const square = yield asyncSquare(sum, sum)
return square
})
const partialAsyncSumAndSquare = x => y => asyncSumAndSquare(x, y)
const result = await partialAsyncSumAndSquare(2)(2)
t.is(result, 16)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment