Skip to content

Instantly share code, notes, and snippets.

@burdiuz
Last active January 12, 2022 11:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save burdiuz/5bcf6377c2385c0954af925518fa37e7 to your computer and use it in GitHub Desktop.
Save burdiuz/5bcf6377c2385c0954af925518fa37e7 to your computer and use it in GitHub Desktop.
@actualwave/promised-timeout - Function that returns a Promise which will be resolved on timeout

@actualwave/promised-timeout

Set of functions that return promise which will be resolved after a specific timeout. It best suited to use with async/await.

import timeout from '@actualwave/promised-timeout';

async function test() {
	console.log('Before');
	
	await timeout(1000);
	
	console.log('After a second');
}

Installation

Via NPM

npm install @actualwave/promised-timeout --save

Or yarn

yarn add @actualwave/promised-timeout

API

Functions available

  • timeout() -- Present as default or named export, resolves after specified number of milliseconds
  • aTick -- Returns a promise which resolves on next tick
  • aQuarterSecond -- Returns a promise which resolves after a quarter of second
  • aHalfSecond -- Returns a promise which resolves after a half of second
  • aSecond -- Returns a promise which resolves after a second
  • aQuarterMinute -- Returns a promise which resolves after a quarter of minute
  • aHalfMinute -- Returns a promise which resolves after a half of minute
  • aMinute -- Returns a promise which resolves after a minute
  • aQuarterHour -- Returns a promise which resolves aftera quarter of hour
  • aHalfHour -- Returns a promise which resolves after a half of hour
  • anHour -- Returns a promise which resolves after an hour

These functions with predefined timeouts should cover majority of timeout() usages:

import { aSecond, aTick } from '@actualwave/promised-timeout';

async function test() {
	console.log('Before');
	
	await aSecond();
	
	console.log('After a second');
	
	await aTick();
	
	console.log('Next tick');
}

Constants available

  • QUARTER_SECOND -- 250, quarter of second in milliseconds
  • HALF_SECOND -- 500, half of second in milliseconds
  • SECOND -- 1000, second in milliseconds
  • QUARTER_MINUTE -- 15000, quarter of minute in milliseconds
  • HALF_MINUTE -- 30000, half of minute in milliseconds
  • MINUTE -- 60000, minute in milliseconds
  • QUARTER_HOUR -- 900000, quarter of hour in milliseconds
  • HALF_HOUR -- 1800000, half of hour in milliseconds
  • HOUR -- 3600000, an hour in milliseconds

Constants can be used with timeout() function.

import timeout, { SECOND } from '@actualwave/promised-timeout';

async function test() {
	console.log('Before');
	
	await timeout(SECOND);
	
	console.log('After a second');
}
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
export declare function timeout(duration: number): Promise<number>;
export declare const QUARTER_SECOND: number;
export declare const HALF_SECOND: number;
export declare const SECOND: number;
export declare const QUARTER_MINUTE: number;
export declare const HALF_MINUTE: number;
export declare const MINUTE: number;
export declare const QUARTER_SECOND: number;
export declare const QUARTER_SECOND: number;
export declare const QUARTER_HOUR: number;
export declare const HALF_HOUR: number;
export declare const HOUR: number;
export declare function aTick(): Promise<number>;
export declare function aQuarterSecond(): Promise<number>;
export declare function aHalfSecond(): Promise<number>;
export declare function aSecond(): Promise<number>;
export declare function aQuarterMinute(): Promise<number>;
export declare function aHalfMinute(): Promise<number>;
export declare function aMinute(): Promise<number>;
export declare function aQuarterHour(): Promise<number>;
export declare function aHalfHour(): Promise<number>;
export declare function anHour(): Promise<number>;
export default timeout;
export const timeout = (duration) => new Promise((resolve) => setTimeout(resolve, duration, duration));
export const QUARTER_SECOND = 250;
export const HALF_SECOND = 500;
export const SECOND = 1000;
export const QUARTER_MINUTE = 15000;
export const HALF_MINUTE = 30000;
export const MINUTE = 60000;
export const QUARTER_HOUR = 900000;
export const HALF_HOUR = 1800000;
export const HOUR = 3600000;
export const aTick = () => timeout(0);
export const aQuarterSecond = () => timeout(QUARTER_SECOND);
export const aHalfSecond = () => timeout(HALF_SECOND);
export const aSecond = () => timeout(SECOND);
export const aQuarterMinute = () => timeout(QUARTER_MINUTE);
export const aHalfMinute = () => timeout(HALF_MINUTE);
export const aMinute = () => timeout(MINUTE);
export const aQuarterHour = () => timeout(QUARTER_HOUR);
export const aHalfHour = () => timeout(HALF_HOUR);
export const anHour = () => timeout(HOUR);
{
"name": "@actualwave/promised-timeout",
"description": "Function that returns a Promise which will be resolved on timeout",
"version": "0.0.7",
"main": "index.js",
"module": "index.js",
"typings": "index.d.ts",
"type": "module",
"keywords": [
"promise",
"async",
"await",
"timeout",
"setTimeout",
"then",
"catch",
"resolve",
"reject"
],
"author": {
"name": "Oleg Galaburda",
"email": "burdiuz@gmail.com",
"url": "http://actualwave.com/"
},
"bugs": {
"url": "https://gist.github.com/burdiuz/5bcf6377c2385c0954af925518fa37e7",
"email": "burdiuz@gmail.com"
},
"homepage": "https://gist.github.com/burdiuz/5bcf6377c2385c0954af925518fa37e7",
"license": "MIT"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment