[nodejs]异步sleep和同步sleep(Asynchronous/Synchronous )

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/wag2765/article/details/84764065

同步睡眠(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;    
}

 

 

展开阅读全文

没有更多推荐了,返回首页