Socket
Socket
Sign inDemoInstall

waitasecond

Package Overview
Dependencies
Maintainers
1
Versions
112
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

waitasecond

Simple tool library for the waiting using Promises.


Version published
Weekly downloads
767
decreased by-39.13%
Maintainers
1
Weekly downloads
 
Created
Source

⏰ Waitasecond

Waitasecond is an extremely simple and elegant tool for using working with async code and Promises. It is usable by browser, worker and node environment and fully typed.

🔥 Install

Install from NPM

npm i waitasecond

🕛 Await forTime (setTimeout equivalent)

In JavaScript there is very elegant syntax to write asynchronous code with async/await. 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 immediatelly.`);
await forTime(500);
console.log(`⌛ And this after 500 miliseconds.`);
await forTime(666);
console.log(`😈 Wow, I have escaped from callback hell`);

🕧 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 non-standard feature and it is not working in node, function forImmediate is working in all environments Note: If you want to use equivalent of setInterval, see [https://rxjs.dev/api/index/function/interval).

🕐 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 nicer syntax version of requestAnimationFrame*/
) {
    const now = await forAnimationFrame();

    updateScene(now);
    renderScene(now);
}

Note: This is working only in browser environment.

🕜 Await forEver

Never resolves or rejects. It is elegant way to test what happen if some part of async code stucks (for example some fetch call).

import { forEver } from 'waitasecond';

await forEver();
console.log(`🧟 This will never ever happen.`);

🕑 Await forTimeSynced

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,...`,
    );
}

🕝 Await forValueDefined

import { forValueDefined } from 'waitasecond';

// TODO: !!!

🕒 Await forAllImagesInElement

import { forAllImagesInElement } from 'waitasecond';

// TODO: !!!

🖋️ Contributing

I am opened to your pull requests, feedback, suggestions and donations :) . Contact to me is on my personal page

FAQs

Package last updated on 13 Sep 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc