when-conditional
Advanced tools
Comparing version 1.1.1 to 1.2.0
{ | ||
"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); | ||
25
when.js
@@ -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; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
5180
5
50
109