30-seconds-code——Function

英文文章来源于:https://github.com/Chalarangelo/30-seconds-of-code/blob/master/README.md

Function

chainAsync

异步执行函数链.

循环一个包含异步事件的函数数组, 当异步事件执行完毕后调用 next 方法.

const chainAsync = fns => { let curr = 0; const next = () => fns[curr++](next); next(); };
/*
chainAsync([
  next => { console.log('0 seconds'); setTimeout(next, 1000); },
  next => { console.log('1 second');  setTimeout(next, 1000); },
  next => { console.log('2 seconds'); }
])
*/

curry

函数的柯里化.

用递归.
如果传入的参数对象 (args) 是有效的, 然后调用传入的函数 fn.
否则返回 fn 和剩余参数中它所需的参数.
如果想要柯里化一个接受任意参数的函数(可变参数, e.g. Math.min()), 你需要传入第二个参数 arity.

const curry = (fn, arity = fn.length, ...args) =>
  arity <= args.length
    ? fn(...args)
    : curry.bind(null, fn, arity, ...args);
// curry(Math.pow)(2)(10) -> 1024
// curry(Math.min, 3)(10)(50)(2) -> 2

functionName

记录函数的名字.

console.debug() 和 被传递的方法的 name 属性来记录方法的名字并打印在控制台上.

const functionName = fn => (console.debug(fn.name), fn);
// functionName(Math.max) -> max (logged in debug channel of console)

pipe

按照 left-to-right 的顺序执行函数的集合.

Array.reduce() 和 (...) 操作符按照 left-to-right 的顺序执行函数的集合.
第一个函数可以接受一个或者更多的参数; 其余的函数必须是一元函数.

const pipeFunctions = (...fns) => fns.reduce((f, g) => (...args) => g(f(...args)));
/*
const add5 = x => x + 5
const multiply = (x, y) => x * y
const multiplyAndAdd5 = pipeFunctions(multiply, add5)
multiplyAndAdd5(5, 2) -> 15
*/

更多关于30-seconds-code中文翻译https://github.com/lvzhenbang/article/blob/master/js/30-seconds-code/index.md

compose

按照 right-to-left 的顺序执行函数的集合.

Array.reduce() to perform right-to-left function composition.
最后一个函数可以接受一个或者更多的参数; 其余的函数必须是一元函数.

const compose = (...fns) => fns.reduce((f, g) => (...args) => f(g(...args)));
/*
const add5 = x => x + 5
const multiply = (x, y) => x * y
const multiplyAndAdd5 = compose(add5, multiply)
multiplyAndAdd5(5, 2) -> 15
*/

runPromisesInSeries

执行一组串联的promise.

Array.reduce() 创建一个promise链, 链中每一个promise状态变为resolved,返回下一个promise.

const runPromisesInSeries = ps => ps.reduce((p, next) => p.then(next), Promise.resolve());
// const delay = (d) => new Promise(r => setTimeout(r, d))
// runPromisesInSeries([() => delay(1000), () => delay(2000)]) -> 按顺序的执行promise链,共花费3s的时间

sleep

延迟异步函数的执行.

async 函数的延迟功能部分的实现是通过将该异步函数放入定时器中,然后返回一个 Promise.

const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
/*
async function sleepyWork() {
  console.log('I\'m going to sleep for 1 second.');
  await sleep(1000);
  console.log('I woke up after 1 second.');
}
*/

更多关于30-seconds-code中文翻译https://github.com/lvzhenbang/article/blob/master/js/30-seconds-code/index.md

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值