React에서 동기 방식 delay 사용

2021. 3. 25. 19:15서버 프로그래밍

바람직한 방법이 아닐수 있지만, 강제로 delay를 주고자 할때는 Promise와 await를 이용할 수 있다.

You would be able to create delay function with async

function timeout(delay: number) {
    return new Promise( res => setTimeout(res, delay) );
}

and then call the function

await timeout(1000); //for 1 sec delay

stackoverflow.com/questions/42089548/how-to-add-delay-in-reactjs

 

How to add delay in Reactjs

I am new to Reactjs. I am not sure how to add delay to render in Reactjs.What is the best way to add delay. I am adding the following code in render but its not working. setTimeout(function() { },

stackoverflow.com

 

async componentDidMount() {
    const response = await fetch(`https://api.com/v1/ticker/?limit=10`);
    const json = await response.json();
    this.setState({ data: json });
}

www.valentinog.com/blog/await-react/

 

How To Use Async Await in React (componentDidMount Async)

How to use Async Await in React? In this post we'll see how to fix regeneratorRuntime and and how to handle errors with Fetch and async/await.

www.valentinog.com