
Product
Introducing Pull Request Stories to Help Security Teams Track Supply Chain Risks
Socket’s new Pull Request Stories give security teams clear visibility into dependency risks and outcomes across scanned pull requests.
Using breathe.js, you can easily write nonblocking JavaScript that runs in the main thread of a web page.
With breathe.js, you divide large, processor-intensive functions into smaller tasks that don't run all at once. The library offers a replacement to loops, function calls, and code blocks, automatically exiting a function after a certain amount of time and allowing the webpage to respond, before returning to the function.
As a simple example, in traditional JavaScript, you may have a long looping function:
function longLoopingFunction() {
var i;
for(i = 0; i < 100000; i++) {
trickyFunction();
}
}
Here trickyFunction()
runs 100,000 times, without letting any other code run or UI respond. But with breathe.js, the same code can be written as:
function breathableLongLoopingFunction() {
return breathe.times(100000, function (i) {
trickyFunction();
});
}
Here the function also runs sequentially, but if it runs for too long (over 17 milliseconds by default), breathe.js relinquishes the main thread to allow other functions to run or UI to respond, then runs the remaining loop, repeatedly relinquishing if necessary.
By using promise conventions and nested functions, converting code is usually straightforward, preserving a function's overall structure and logic. Converting code makes it asynchronous, and adds methods to stop, pause, and unpause the code. Read more about how to use it on the 'Using breathe.js' page.
Web workers are designed to run asynchronous, nonblocking code (in another thread, to boot!), but unfortunately they can't do everything. Variables aren't easily shared with a page's main thread, instead relying on message passing. Workers can't acccess DOM, nor can most access a canvas (though there is an OffscreenCanvas in development). Since breathe.js can run inside the main thread of a page, it can access its variables, DOM, and canvases.
Web workers still use a single thread within the worker, so a computation-heavy function can block other code— namely message handling— from running. Breathe.js works within web workers, so they can respond in the middle of executing a long-running function. It also makes it easy to pause and unpause code running within the worker.
console.log
statements or chunks that execute longer than expected, it can make the UI sluggish or nonresponsive. Without the warning, it can be more difficult to stop the page.Check out the 'Examples' page to see what you can do with it, and read the 'Using breathe.js' page for an in-depth explanation.
Breathe.js currently offers three main ways to create breathable code. breathe.chain()
creates a breathable promise chain. As an alternative to while
loops, breathe.loop()
creates an asynchronous loop, with a condition and a body. And breathe.times()
creates a loop with a fixed number of iterations and a body, a replacement for some for
loops.
Large functions can be subdivided into blocks of code, with variable declarations and synchronous and/or asynchronous code.
function () {
var variablesSharedInsideOfThisBlock;
synchronousCode();
return asynchronousCode();
}
You don't need to have both synchronous code or asynchronous code, but asynchronous code usually involves subsequent code blocks. For instance, breathe.loop takes a body as an argument, which is a code block. This allows you to nest loops:
function nestedLoop() {
var running;
running = true;
return breathe.loop(function () { return running; }, function () {
// another code block
var i;
i = 0;
return breathe.loop({ function () { return running && i++ < 50; },
function () {
running = doSomethingAwesome(c);
}
);
}
);
}
You can use the .then() method of promises (the asynchronous code) to chain code blocks together, so you can run code after asynchronous code completes.
function sequentialLoops() {
var i = 0;
return breathe.loop(function () { i++ < 50;}, function () {
console.log('Counting up: ', i);
}
).then(function () {
// another code block
return breathe.loop(function () { i-- >= 0;}, function () {
console.log('Counting down: ', i);
});
});
}
Breathable Chains are similar to traditional promises in that they implement then()
and catch()
methods, though they return the original chain object rather than a new promise. Breathable chains implement additional methods to stop, pause, and unpause promise chains.
# breathe.chain([initValue])
# breathableChain.then(onFulfilled[, onRejected])
Promise.prototype.then()
, except it alters its internal promise chain instead of returning a new promise. Both onFulfilled and onRejected can optionally return a value to pass on to the next promise chain, or a promise (breathable or not), that are resolved or rejected before continuing down the promise chain. Returns the invoking breathableChain.# breathableChain.catch(onRejected)
Promise.prototype.catch()
, except it alters its internal promise chain instead of returning a new promise. onRejected can optionally return a value to pass on to the next promise chain that are resolved or rejected before continuing down the promise chain. Returns the invoking breathableChain.# breathableChain.pause()
# breathableChain.unpause()
# breathableChain.stop()
Breathable Loops are breathable chains that repeatedly iterate over a body while a condition is true. They can be stopped, paused, or unpaused. They can serve as a replacement to while
loops.
# breathe.loop(config)
# breathe.loop(condition, body, [config])
Times Loops are breathable chains that repeatedly iterate over a body for a fixed number of iterations. They can be stopped, paused, or unpaused. They can serve as a replacement to some for
loops.
# breathe.times(config)
# breathe.times(iterations, body, [config])
FAQs
a library to write nonblocking, asynchronous JavaScript
We found that breathe demonstrated a not healthy version release cadence and project activity because the last version was released 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.
Product
Socket’s new Pull Request Stories give security teams clear visibility into dependency risks and outcomes across scanned pull requests.
Research
/Security News
npm author Qix’s account was compromised, with malicious versions of popular packages like chalk-template, color-convert, and strip-ansi published.
Research
Four npm packages disguised as cryptographic tools steal developer credentials and send them to attacker-controlled Telegram infrastructure.