[nodejs] 비동기 sleep 와 동기 sleep (Asynchronous / synchronous)

859 단어 node.js
비동기 수면 (Synchronous Sleep)
방식 1:
 
setTimeout(function () {
  clearInterval(i);
  console.log('end');
}, 100000);

var i = setInterval(function () {
  console.log('ping');
}, 1000);

 
 
방식 2:
 
var i = setInterval(function () {
  console.log('ping');
}, 1000);

sleep(100000);

 
동기 수면 (Asynchronous Sleep) 설치 deasync:
https://www.npmjs.com/package/deasync
function SyncFunction(){
  var ret;
  setTimeout(function(){
      ret = "hello";
  },3000);
  while(ret === undefined) {
    require('deasync').sleep(100);
  }
  // returns hello with sleep; undefined without
  return ret;    
}

 

좋은 웹페이지 즐겨찾기