Socket
Socket
Sign inDemoInstall

when-conditional

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

when-conditional - npm Package Compare versions

Comparing version 1.1.1 to 1.2.0

test/test-clear.js

2

package.json
{
"name": "when-conditional",
"version": "1.1.1",
"version": "1.2.0",
"description": "A simple little asyncronous if check and conditional method",

@@ -5,0 +5,0 @@ "main": "when.js",

@@ -50,2 +50,36 @@ # When-conditional

## Removing the when listener
---------------------
You might feel that you no longer need to wait for something to happen. if this is the case, you can call a `.clear()` function on the return value of `when(condition, code)`.
#### Example
The following example is very similar to the one above, but will never print `someVar is now true, and this was only triggered when it became true!` as it is no longer waiting for `when` someVar is true!
```javascript
var when = require('when-conditional');
var someVar = false;
var interval = setInterval(function(){
console.log("someVar is " + someVar);
if(someVar === true) clearInterval(interval);
}, 1000);
var whenObj = when(function condition(){
return (someVar === true);
}, function code(){
console.log("someVar is now true, and this was only triggered when it became true!");
});
setTimeout(function(){
someVar = true;
}, 10000);
setTimeout(function(){
whenObj.clear();
}, 5000);
```
## Inspiration

@@ -52,0 +86,0 @@ ---------------------

@@ -9,3 +9,3 @@ var when = require('../');

when(function condition(){
var immediate = when(function condition(){
return (someVar === true);

@@ -19,1 +19,2 @@ }, function code(){

}, 10000);

@@ -6,13 +6,20 @@ // the require('setImmediate') adds a setImmediate polyfill to the global

function when(condition, code){
if(condition()){
setImmediate(code);
return;
} else {
setImmediate(function(){
when(condition, code);
});
return;
}
var immediate = setImmediate(checkCondition);
function checkCondition(){
if(condition()){
immediate = setImmediate(code);
} else {
immediate = setImmediate(checkCondition);
}
}
immediate.clear = function(){
console.log(immediate);
clearImmediate(immediate);
}
return immediate;
};
module.exports = exports = when;
SocketSocket SOC 2 Logo

Product

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

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc