UNPKG

extra-async-function

Version:

An async function is a function that delivers its result asynchronously (through Promise).

191 lines (188 loc) 5.22 kB
function COMPARE$1(a, b) { return a < b ? -1 : (a > b ? 1 : 0); } function name(x) { return x.name; } function length(x) { return x.length; } function bind(x, ths, ...prefix) { return x.bind(ths, ...prefix); } function call(x, ths = null, ...args) { return x.call(ths, ...args); } function apply(x, ths = null, args) { return x.apply(ths, args); } function isAsync(v) { const AsyncFunction = (async function () { }).constructor; return v instanceof AsyncFunction; } function isGenerator(v) { const GeneratorFunction = (function* () { }).constructor; return v instanceof GeneratorFunction; } function contextify(x) { return function (...args) { return x(this, ...args); }; } function decontextify(x) { return (ths = null, ...args) => x.call(ths, ...args); } function reverse(x) { return (...args) => x(...args.reverse()); } function spread(x) { return (...args) => x(args); } function unspread(x) { return (args) => x(...args); } function attach(x, ...prefix) { return (...args) => x(...prefix, ...args); } function attachRight(x, ...suffix) { return (...args) => x(...args, ...suffix); } function curry(x, n = x.length) { return (...args) => { if (args.length >= n) return x(...args); else return curry((...rest) => x(...args, ...rest), n - args.length); }; } function curryRight(x, n = x.length) { return curry(reverse(x), n); } function defer(x) { return (...args) => { var h = setImmediate(flush); function clear() { clearImmediate(h); h = null; } function flush() { x(...args); clear(); } return { clear, flush }; }; } function delay(x, t) { return (...args) => { var h = setTimeout(flush, t); function clear() { clearTimeout(h); h = null; } function flush() { x(...args); clear(); } return { clear, flush }; }; } function restrict(x, start, end = -1) { var i = -1; return (...args) => { if ((++i < start) === (i < end || end < 0)) return; return x(...args); }; } function restrictOnce(x) { return restrict(x, 0, 1); } function restrictBefore(x, n) { return restrict(x, 0, n); } function restrictAfter(x, n) { return restrict(x, n); } function debounce(x, t, T = -1) { var savedArgs; var h = null, H = null; function clear() { clearTimeout(h); clearTimeout(H); h = H = null; } function flush() { x(...savedArgs); clear(); } return (...args) => { savedArgs = args; if (T >= 0) H = H || setTimeout(flush, T); if (T < 0 || t < T) { clearTimeout(h); h = setTimeout(flush, t); } return { clear, flush }; }; } function debounceEarly(x, t, T = -1) { var h = null, H = null; function clear() { h = H = null; } function flush() { clear(); } return (...args) => { if (!h && !H) x(...args); if (T >= 0) H = H || setTimeout(flush, T); if (T < 0 || t < T) { clearTimeout(h); h = setTimeout(flush, t); } return { clear, flush }; }; } function throttle(x, t) { var savedArgs; var h = null; function clear() { h = null; } function flush() { x(...savedArgs); clear(); } return (...args) => { savedArgs = args; h = h || setTimeout(flush, t); return { clear, flush }; }; } function throttleEarly(x, t) { var h = null; function clear() { h = null; } function flush() { clear(); } return (...args) => { if (!h) x(...args); h = h || setTimeout(flush, t); return { clear, flush }; }; } function ARGUMENTS(...args) { return Promise.all(args); } async function NOOP(...args) { } async function IDENTITY(v) { return v; } async function COMPARE(a, b) { return COMPARE$1(await a, await b); } function negate(x) { return async (...args) => !(await x(...args)); } function memoize(x, fr = null, cache = null) { var fr = fr || IDENTITY; var cache = cache || new Map(); return async (...args) => { var k = await fr(...args); if (cache.has(k)) return cache.get(k); var v = await x(...args); cache.set(k, v); return v; }; } function compose(...xs) { return composeRight(...xs.reverse()); } function composeRight(...xs) { return async (...args) => { if (xs.length === 0) return; var a = await xs[0](...args); for (var i = 1, I = xs.length; i < I; i++) a = await xs[i](a); return a; }; } export { ARGUMENTS, COMPARE, IDENTITY, NOOP, restrictAfter as after, apply, length as arity, attach, attachRight, restrictBefore as before, bind, call, compose, composeRight, contextify, curry, curryRight, debounce, debounceEarly, decontextify, defer, delay, reverse as flip, isAsync as is, isGenerator, length, memoize, name, negate, restrictOnce as once, attach as partial, attachRight as partialRight, restrict, restrictAfter, restrictBefore, restrictOnce, reverse, spread, throttle, throttleEarly, unspread };