npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

waitasecond

v1.11.117

Published

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 l

Downloads

6,590

Readme

⏰ 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

           

Become a partner