
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
waitasecond
Advanced tools
Wait a second 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 from NPM
npm i waitasecond
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`);
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.
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.
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.`);
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,...`,
);
}
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.
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);
// ...
I am opened to your pull requests, feedback, suggestions, and donations. Contact to me is on my personal page
FAQs
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
We found that waitasecond demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
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.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.