A helper for pausing functions.
constasynclock=require("asynclock")constal=asynclock();constsend=(data)=>{constRandom=123;// Generate Random IDconstPack=JSON.stringify({id:Random, data});// Pack Data with Id// Send Data Package// Socket.send(Pack);setTimeout(function(){onmessage(Pack);},1000);// For simulating response// Return Lockreturnal.Lock(Random);}constonmessage=(Pack)=>{constUnpacked=JSON.parse(Pack);al.UnLock(Unpacked.id,Unpacked.data);}consttest=asyncfunction(){constretValue=awaitsend({query:"getUsers"});console.log("retValue",retValue);}test();
{Lock:function(Id){},// (Promise) call when you need to pauseUnLock:function(Id,Value){}// (undefined) call when you need to continue}
Id can be any javascript object key.