Skip to content

Waitasecond is a javascript library that makes managing async code and Promises simple and elegant. It provides an await-like API that can be used in the browser, worker, and node environment, making it a powerful tool for any project. Additionally, the library is fully typed, allowing developers to have complete confidence in the reliability of th

License

Notifications You must be signed in to change notification settings

hejny/waitasecond

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Jan 16, 2025
0cabf32 ยท Jan 16, 2025
Apr 15, 2024
May 3, 2023
Sep 12, 2021
Jan 16, 2025
Sep 13, 2021
Sep 17, 2021
Jan 16, 2025
Sep 13, 2021
Sep 17, 2021
Dec 26, 2020
Feb 2, 2022
Dec 24, 2023
Apr 15, 2024
Sep 13, 2021
Feb 19, 2021
Sep 23, 2024
Jan 1, 2025
Sep 10, 2021
Sep 13, 2021
Jan 16, 2025
Jan 16, 2025
Sep 17, 2021
Jul 4, 2019

Repository files navigation

โฐ Waitasecond

NPM Version of Waitasecond Quality of package Waitasecond Known Vulnerabilities Issues Cypress.io Socket

Waitasecond is a javascript library that makes managing async code and Promises simple and elegant. It provides an await-like API that can be used in the browser, worker, and node environment, making it a powerful tool for any project. Additionally, the library is fully typed, allowing developers to have complete confidence in the reliability of their code. With Waitasecond, developers can easily and reliably handle any asynchronous task with ease.

Wallpaper of โฐ Waitasecond

๐Ÿ”ฅ Install

Install from NPM

npm i waitasecond

๐Ÿ•› Await forTime (setTimeout equivalent)

In JavaScript, there is an elegant way how to write asynchronous code with async/await syntax construct. Every internal function and library is heading forward to be compatible with Promises and deprecating its old callback type. But there are some relicts from callback hell like setTimeout, requestAnimationFrame,.... Waitasecond has motivation to turn this into elegant syntax:

import { forTime } from 'waitasecond';

console.log(`โณ This is logged immediately.`);
await forTime(500);
console.log(`โŒ› And this after 500 milliseconds.`);
await forTime(666);
console.log(`๐Ÿ˜ˆ Wow, I have escaped from callback hell`);

๐Ÿ“–Documentation ๐Ÿ’ปCode

๐Ÿ•ง Await forImmediate (setImmediate equivalent)

import { forImmediate } from 'waitasecond';

async function doSomething() {
    console.log(`๐Ÿ foo`);
    await forImmediate();
    console.log(`๐ŸŽ bar`);
}

doSomething();
doSomething();

// ๐Ÿ foo
// ๐Ÿ foo
// ๐ŸŽ bar
// ๐ŸŽ bar

await doSomething();
await doSomething();

// ๐Ÿ foo
// ๐ŸŽ bar
// ๐Ÿ foo
// ๐ŸŽ bar

Note: Despite window.setImmediate is a non-standard feature and it is not working in node, function forImmediate is working in all environments Note: If you want to use an equivalent of setInterval, see RxJS interval.

๐Ÿ“–Documentation ๐Ÿ’ปCode

๐Ÿ• Await forAnimationFrame (requestAnimationFrame equivalent)

With forAnimationFrame you can write nice looking render/update/whatever loops.

import { forAnimationFrame } from 'waitasecond';

while (
    true /* โ† Normally, this would be ๐Ÿ’ฉ code, but with forAnimationFrame it is a nicer syntax version of requestAnimationFrame*/
) {
    const now = await forAnimationFrame();

    updateScene(now);
    renderScene(now);
}

Note: This is working only in a browser environment.

๐Ÿ“–Documentation ๐Ÿ’ปCode

๐Ÿ•œ Await forEver

forEver function returns a promise which never resolves or rejects. It is an elegant way to test what happened if some part of asynchronous code stuck (for example, fetch call).

import { forEver } from 'waitasecond';

await forEver();
console.log(`๐ŸงŸ This will never ever happen.`);

๐Ÿ“–Documentation ๐Ÿ’ปCode

๐Ÿ•‘ Await forTimeSynced

forTimeSynced is an ideal way how to do periodical ticking in an unstable environment. For example, if you want to run a process every 10 minutes on a server, but PM2 is restarting a server unexpectedly.

import { forTimeSynced } from 'waitasecond';

while (true) {
    await forTimeSynced(10 /* Minutes */ * 60 * 1000);
    console.log(
        `โŒ› This will be logged every 10 minutes according to computer time. So it fires for example on 12:00, 12:10, 12:20,...`,
    );
}

๐Ÿ“–Documentation ๐Ÿ’ปCode

๐Ÿ• Await forValueDefined

import { forValueDefined } from 'waitasecond';

const firstName = forValueDefined(() => data.firstName);

Note: This is not definitely the ideal way how to wait for things. But it can be helpful if you want to "observe" some mutating object which do not support it natively.

๐Ÿ“–Documentation ๐Ÿ’ปCode

๐Ÿ•’ Await forImage

import { forImage } from 'waitasecond';

await forImage(document.querySelector('.logo'));
console.log(`๐Ÿ–ผ๏ธ Now I can be sure that the logo is loaded.`);

// ...
await renderToPdf(document.body);
// ...

๐Ÿ“–Documentation ๐Ÿ’ปCode

๐Ÿ•’ Await forAllImagesInElement

import { forAllImagesInElement } from 'waitasecond';

await forAllImagesInElement(document.body);
console.log(`๐Ÿ–ผ๏ธ Now I can be sure that all images in body are loaded.`);

// ...
await renderToPdf(document.body);
// ...

๐Ÿ“–Documentation ๐Ÿ’ปCode

๐Ÿ–‹๏ธ Contributing

I am open to pull requests, feedback, and suggestions. Or if you like this utility, you can โ˜• buy me a coffee or donate via cryptocurrencies.

You can also โญ star the waitasecond package, follow me on GitHub or various other social networks.

โœจ Partners

Collboard       WebGPT       Promptbook

Become a partner

About

Waitasecond is a javascript library that makes managing async code and Promises simple and elegant. It provides an await-like API that can be used in the browser, worker, and node environment, making it a powerful tool for any project. Additionally, the library is fully typed, allowing developers to have complete confidence in the reliability of th

Topics

Resources

License

Stars

Watchers

Forks