const debounce = ( fn, time ) => {
let timerId;
return ( ...args ) => {
if ( timerId ) {
clearTimeout( timerId );
}
timerId = setTimeout( () => {
fn( ...args );
timerId = null;
}, time );
};
}
const fn = (arg) => {
console.log(arg)
}
const debouncedFn = debounce(fn, 250)
let i = 0
debouncedFn(++i)
debouncedFn(++i)
debouncedFn(++i)
debouncedFn(++i)
function promisify(obj) {
const cache = {}
return new Proxy(obj, {
get(target, prop) {
const value = target[prop]
const type = Object.prototype.toString.call(value)
if (type === '[object Object]') {
if (!cache[prop]) {
cache[prop] = promisify(value)
}
return cache[prop]
} else if (type === '[object Function]') {
if (!cache[prop]) {
cache[prop] = function (...args) {
return new Promise((resolve, reject) => {
const callback = (err, result) => {
if (err) {
reject(err)
} else {
resolve(result)
}
}
value.call(this, callback, ...args)
})
}
}
return cache[prop]
}
return value
},
})
}
const SignIn = ({ isAuth }) => {
useEffect(() => {
if (isAuth) {
history.push('/')
}
}, [isAuth])
}
<Route ... component={SignIn} />
=> props.history