Skip to content

Instantly share code, notes, and snippets.

@sbrl
Created May 29, 2019 23:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sbrl/c485d924964fa54c233bb1e275a28909 to your computer and use it in GitHub Desktop.
Save sbrl/c485d924964fa54c233bb1e275a28909 to your computer and use it in GitHub Desktop.
"use strict";
async function pipe(initial_value, ...funcs) {
let current = initial_value;
for(let func of funcs) {
current = func(current);
// If it's thenable, then it's probably a Promise
if(typeof current.then == "function")
current = await current;
}
return current;
}
function add(a, b) { return a + b; }
function make_message(x) { return `item: ${x}`; }
async function tokenise(x) { return x.split(/\s+/g); }
pipe(
10,
add.bind(null, 5),
make_message,
tokenise
).then(console.log);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment