https://github.com/zoubin/when-ready
Increasing delays dynamically
https://github.com/zoubin/when-ready
Last synced: over 1 year ago
JSON representation
Increasing delays dynamically
- Host: GitHub
- URL: https://github.com/zoubin/when-ready
- Owner: zoubin
- License: mit
- Created: 2015-12-04T06:19:52.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2015-12-04T10:26:42.000Z (over 10 years ago)
- Last Synced: 2025-02-09T17:05:45.393Z (over 1 year ago)
- Language: JavaScript
- Size: 4.88 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# when-ready
[](https://www.npmjs.org/package/when-ready)
[](https://travis-ci.org/zoubin/when-ready)
[](https://coveralls.io/github/zoubin/when-ready)
[](https://david-dm.org/zoubin/when-ready)
[](https://david-dm.org/zoubin/when-ready#info=devDependencies)
Sugar way to manage your pendings.
## Example
```javascript
var Ready = require('..')
function Concat() {
this.ready = Ready()
}
Concat.prototype.add = function(file) {
this['addType' + (rand() % 2 + 1)](file)
}
Concat.prototype.addType1 = function(file) {
var self = this
this.ready.delay(
new Promise(function (resolve) {
setTimeout(function() {
self.push('{' + file.slice(-1) + '}')
resolve()
}, rand())
})
)
}
Concat.prototype.addType2 = function(file) {
var cb = this.ready.delay()
var self = this
setTimeout(function() {
self.push('[' + file.slice(-1) + ']')
cb()
}, rand())
}
Concat.prototype.push = function(source) {
console.log(source.toUpperCase())
}
Concat.prototype.end = function() {
var self = this
this.ready.then(function () {
self.push('\n# end #')
})
}
function rand() {
return Math.ceil(Math.random() * 100)
}
var concat = new Concat()
concat.add('./a')
concat.add('./b')
concat.add('./c')
concat.end()
// [B]
// {C}
// [A]
//
// # END #
```
## class Ready()
Instances are `thenable` objects.
### .then(onFulfilled, onRejected)
### .catch(onRejected)
### .delay(n)
If `n` is `Number`, it specifies the amount of time to be delayed.
If `n` is `Promise`, it delays until the promise resolves or rejects.
Otherwise, it delays until the returned callback is called.