
Research
Two Malicious Rust Crates Impersonate Popular Logger to Steal Wallet Keys
Socket uncovers malicious Rust crates impersonating fast_log to steal Solana and Ethereum wallet keys from source code.
yieldtomain
Advanced tools
A utility function to yield execution to the main thread in JavaScript, preventing UI blocking. Uses scheduler.yield()
when available, falling back to setTimeout
.
The gist is this:
function yieldToMain() {
const scheduler = globalThis.scheduler;
if (scheduler && typeof scheduler.yield === 'function') {
return scheduler.yield();
}
return new Promise((resolve) => {
setTimeout(resolve, 0);
});
}
Feel free to copy and paste this into your codebase. Or continue with using it as a module etc., as described below.
npm install yieldtomain
The library can be used in a bunch o' JavaScript environments:
import yieldToMain from 'yieldtomain';
async function processItems(items) {
for (const item of items) {
await yieldToMain();
// Process item
}
}
const yieldToMain = require('yieldtomain');
async function heavyTask() {
for (let i = 0; i < 1000000; i++) {
if (i % 1000 === 0) await yieldToMain();
// Compute something
}
}
<script src="dist/yieldToMain.browser.js"></script>
<script>
async function nonBlockingTask() {
for (let i = 0; i < 1000000; i++) {
if (i % 1000 === 0) await yieldToMain();
// Heavy computation
}
}
</script>
Check out the examples/heavy.html
demo to see the difference between blocking and non-blocking computations. The demo includes:
A live version of the demo is right here.
The library is distributed in multiple formats:
dist/yieldToMain.esm.js
- ECMAScript Moduledist/yieldToMain.cjs.js
- CommonJSdist/yieldToMain.browser.js
- Minified browser build with an IIFE that provides a global yieldToMain()
functionAll builds include source maps and TypeScript type definitions.
function yieldToMain(): Promise<void>;
The function returns a Promise that resolves after yielding execution to the main thread. It uses:
scheduler.yield()
if availablesetTimeout(0)
fallback# Install dependencies
npm install
# Build all formats and types
npm run build
# Run tests
npm test
# Run tests in watch mode
npm run test:watch
MIT
The yieldToMain()
function is in no way my invention, I was just surprised it's not widely available in npm, so I decided to create a package for it. It's like a folk song that everyone knows, and yet it's origins are a little mirky. For more nitty-gritty details here's a selection of light (haha!) reading:
FAQs
A utility function to yield to the main thread in JavaScript
We found that yieldtomain demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer 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.
Research
Socket uncovers malicious Rust crates impersonating fast_log to steal Solana and Ethereum wallet keys from source code.
Research
A malicious package uses a QR code as steganography in an innovative technique.
Research
/Security News
Socket identified 80 fake candidates targeting engineering roles, including suspected North Korean operators, exposing the new reality of hiring as a security function.