From 1964dc8c11c34ad0b555fa5db401271b448c19c4 Mon Sep 17 00:00:00 2001 From: Rafa Mel Date: Wed, 17 Feb 2021 13:49:42 +0100 Subject: [PATCH] feat(tasks): adds sleep task --- src/tasks/create/index.ts | 1 + src/tasks/create/sleep.ts | 17 +++++++++++++++++ 2 files changed, 18 insertions(+) create mode 100644 src/tasks/create/sleep.ts diff --git a/src/tasks/create/index.ts b/src/tasks/create/index.ts index fe92305..ceb82ce 100644 --- a/src/tasks/create/index.ts +++ b/src/tasks/create/index.ts @@ -1,3 +1,4 @@ export * from './clear'; export * from './log'; export * from './print'; +export * from './sleep'; diff --git a/src/tasks/create/sleep.ts b/src/tasks/create/sleep.ts new file mode 100644 index 0000000..0b89418 --- /dev/null +++ b/src/tasks/create/sleep.ts @@ -0,0 +1,17 @@ +import { Task, Context } from '../../definitions'; +import { log } from './log'; +import { into } from 'pipettes'; + +export function sleep(ms: number): Task.Async { + return async (ctx: Context): Promise => { + into(ctx, log('debug', `Sleep for ${ms}ms`)); + + return new Promise((resolve) => { + const timeout = setTimeout(() => resolve(), ms); + ctx.cancellation.finally(() => { + clearTimeout(timeout); + resolve(); + }); + }); + }; +}