【nodejs异步部分复习】1.高阶函数来解决异步
高阶函数:参数是一个函数, 或者返回一个函数
应用场景
before
1 | function say(...args){ |
函数柯里化
判断变量的类型四种方法:
- typeof 缺点:不能判断引用类型,都是object
- constructor 可以找到这个变量是谁构造出来的 ({}).constructor
- instanceof 判断谁是谁的实例,原理通过proto上面有没有包含
- Object.prototype.toString.call() 缺点,不能细分谁是谁的实例,
1 | function isType(type){ |
- 实现一个通用的柯里化
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24function isType(type, value) {
console.log(type, value, Object.prototype.toString.call(value));
return Object.prototype.toString.call(value) === `[object ${type}]`;
}
function currying(fn, arr = []) {
let len = fn.length;
return function (...args) {
arr = [...arr, ...args];
if (arr.length < len) {
return currying(fn, arr); // 递归,不停的产生函数
} else {
return fn(...arr);
}
}
}
let isArray = currying(isType)('Array');
let isString = currying(isType)('String');
console.log(isArray([]));
console.log(isString('111'));
after
1 | function after(times, callback) { |
本文作者: haise
本文地址: https://www.shifeng1993.com/2021/01/02/node_async1/
版权声明: 转载请注明出处!