• Home
  • About
    • lahuman photo

      lahuman

      열심히 사는 아저씨

    • Learn More
    • Facebook
    • LinkedIn
    • Github
  • Posts
    • All Posts
    • All Tags
  • Projects

nodejs에서 sleep 구현하기

09 Mar 2019

Reading time ~1 minute

nodejs에서 sleep 구현하기

nodejs에서 for문에 비동기 처리가 들어 있으면 해당 비동기 처리가 종료 되는 것을 기다려 주지 않는다.

따라서 Sleep 같은 처리를 하려면 많은 신경을 써야 한다.

참고 자료를 기반으로 처리해보니 잘된다.

내가 하려는데도 문제 없이 반영되는지 월요일에 확인해봐야겠다.

const init = async () => {
    for (let i=0; i<5; i++){
        console.log(1);
        await sleep(1000);
        console.log(2);
    }
 }
 const sleep = (ms) => {
     return new Promise(resolve=>{
         setTimeout(resolve,ms)
     })
 }

init();

쉬운게없다

참고 주소

  • How Can I Wait In Node.js (Javascript), l need to pause for a period of time


nodejssleep Share Tweet +1