How to create a sleep/delay in nodejs that is Blocking?

后端 未结 12 638
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-02 07:00

I\'m currently trying to learn nodejs and a small project I\'m working is writing an API to control some networked LED lights.

The microprocessor controlling the LED

12条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-02 07:21

    I found something almost working here https://stackoverflow.com/questions/21819858/how-to-wrap-async-function-calls-into-a-sync-function-in-node-js-or-ja vascript

    `function AnticipatedSyncFunction(){
        var ret;
        setTimeout(function(){
            var startdate = new Date()
            ret = "hello" + startdate;
        },3000);
        while(ret === undefined) {
           require('deasync').runLoopOnce();
        }
        return ret;    
    }
    
    
    var output = AnticipatedSyncFunction();
    var startdate = new Date()
    console.log(startdate)
    console.log("output="+output);`
    

    The unique problem is the date printed isn't correct but the process at least is sequential.

提交回复
热议问题