export async function Sleep(ms: number) {
return new Promise(resolve => setTimeout(resolve, ms));
}
//==========================================================================================
/** Sleep: wait / stop 가능한 핸들 반환
* [사용법]
* const sleep = SleepWithHandles(1000);
* await sleep.waitHandle;
*///========================================================================================
export function SleepWithHandles(ms: number) {
let timeoutId: any;
let resolveWait: any;
const waitHandle = new Promise(resolve => {
timeoutId = setTimeout(resolve, ms);
resolveWait = resolve;
});
return {
waitHandle,
stopHandle: function() {
resolveWait();
clearTimeout(timeoutId);
}
};
}