Skip to content

weo-edu/po

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Codeship Status for weo-edu/po

Po is simplified version of Vo that has a promise centric api.

Features

  • Composable parallel and series flows.
  • Super simple

Installation

$ npm install po

Usage

Punk

A punk is po's atom. It's a function that returns a promise. Po takes punks as args and returns a punk. Punks that do not initiate a pipeline must only take one arg.

function punk(arg) {
  return Promise.resolve(arg);
}

Basic

var request = require('superagent-promise');
var po = require('po');

po(function(url) {
  return request('get', url).end();
})('http://weo.io').then(function() {
  //...
});

Pipeline

var request = require('superagent-promise');
var po = require('po');
var db = require('ioredis');

function get(url) {
  return request('get', url).end();
}

function cache(res) {
  return db.set(res.url, res.body);
}

po(get, cache)('http://weo.io').then(function() {
  //...
});

Parallel

var request = require('superagent-promise');
var po = require('po');

function get(url) {
  return request('get', url).end();
}

po([
  get('https://weo.io'),
  get('https://google.com')
])().then(function(responses) {
  //...
});

Parallel Object

var request = require('superagent-promise');
var po = require('po');

function get(url) {
  return request('get', url).end();
}

po({
  weo: get('https://weo.io'),
  google: get('https://google.com')
})().then(function(responses) {
  // `responses` is object
});

Composition

var request = require('superagent-promise');
var cheerio = require('cheerio');
var po = require('po');

function get(url) {
  return request.get(url).end();
}

function title(res) {
  var $ = cheerio.load(res.text);
  return $('title').text();
}

var req = po(get, title);

po({
  weo: req('http://weo.io'),
  google: req('http://google.com')
})().then(function(res) {
  // `res` is an object containing each title
})

Currying

Po returns an auto currying punk. The arity of a po pipeline is defined by the arity of the first punk in the pipeline. The po pipeline is not actually executed until sufficient args are passed.

In the example below req has an arity of 2, and weoGoogle has an arity of 1.

var request = require('superagent-promise');
var cheerio = require('cheerio');
var po = require('po');

function get(url, data) {
  return request.get(url, data).end();
}

function title(res) {
  var $ = cheerio.load(res.body);
  return $('title').text();
}

var req = po(get, title);

var weoGoole = po({
  weo: req('http://weo.io'),
  google: req('http://google.com')
});

weoGoole({limit: 10}).then(function(res) {
  // `res` is an object containing the text of 10 titles for each site
})