New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

continuable-fp

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

continuable-fp

Continuable operators, but with the arguments flipped so they compose better.

latest
Source
npmnpm
Version
0.0.1
Version published
Maintainers
1
Created
Source

continuable fp

Continuable operators, but with the arguments flipped so they compose better, and also they curry automatically. So this is operator(function, data).

This is a fun way to play with IO and functional patterns.

install

npm i -S continuable-fp

example

var compose = require('compose-function')
var c = require('./')

var excitedly = compose(
    // remove a level of nesting
    c.join,
    c.map(function (data) {
        // return a nested continuable
        // here you can make a call to a new continuable (`someIO` below),
        // using the data inside this continuable
        return c.either(
            // call this if `someIO` returns an error
            function onErr (err) {
                return c.of('booo')
            },
            function (val) {
                return c.of(val)
            },
            someIO(data + ' wooo')
        )
    }),
    c.map(function (data) {
        return data + '!!!'
    })
)

excitedly(someIO('this is a value'))(function (err, val) {
    console.log(err, val)
    // null 'this is a value!!! wooo'
})

function someIO (data, cb) {
    if (!cb) return function (_cb) {
        return someIO(data, _cb)
    }
    process.nextTick(function () {
        cb(null, data)
    })
}

function ioError (data, cb) {
    if (!cb) return function (_cb) {
        return ioError(data, _cb)
    }
    process.nextTick(function () {
        cb(new Error('rarrr'))
    })
}

example of errors

var c = require('./')

var myFn = c.either(
    function onErr (err) {
        return c.of('baaaa')
    },
    function onData (data) {
        return c.of('ok')
    }
)

myFn(someIO('woooo'))(function (err, done) {
    console.log('it worked...', err, done)
})

myFn(ioError('booo'))(function (err, ok) {
    console.log('err result...', err, ok)
})

function someIO (data, cb) {
    if (!cb) return function (_cb) {
        return someIO(data, _cb)
    }
    process.nextTick(function () {
        cb(null, data)
    })
}

function ioError (data, cb) {
    if (!cb) return function (_cb) {
        return ioError(data, _cb)
    }
    process.nextTick(function () {
        cb(new Error('rarrr'))
    })
}

typescript

You can use typescript too

npm install -g typescript
tsc ts-example.ts
import c = require('continuable-fp')

var test: c.Continuable<string> = c.of('hello')

c.of('hello')(function (err, val) {
    val.thisdoesnotexist()
    // Property 'thisdoesnotexist' does not exist on type 'string'
})

operators

join

Take a nested continuable and return the inner one

map

Map a value through a predicate function

either

Return the left function if there is an error, right function if there's no error.

either (left, right, continuable)

of

Take a value, and create a continuable of it

Keywords

continuable

FAQs

Package last updated on 24 Jun 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts