Skip to content

Instantly share code, notes, and snippets.

Avatar
🎯
Focusing

Shuaiqi Wang myWsq

🎯
Focusing
View GitHub Profile
View debounce.ts
export function debounce<T extends (...args: any[]) => any>(
func: T,
wait: number,
immediate = false
) {
let timeout: number | undefined;
return function (this: any, ...args: Parameters<T>) {
clearTimeout(timeout);
timeout = setTimeout(() => {
timeout = undefined;
@myWsq
myWsq / use-async-react.js
Last active Apr 7, 2020
[useAsync] Keep track of the state of async function. #Vue #React #Hooks #CompositionAPI #Async #AsyncData #AsyncFunction
View use-async-react.js
import { useState, useEffect, useCallback } from 'React'
const useAsync = (asyncFunc, immediate = true) => {
const [pending, setPending] = useState(0);
const [value, setValue] = useState(null);
const [error, setError] = useState(null);
const execute = useCallback(() => {
setPending(val => val + 1);
setValue(null);