
Security News
AI Agent Lands PRs in Major OSS Projects, Targets Maintainers via Cold Outreach
An AI agent is merging PRs into major OSS projects and cold-emailing maintainers to drum up more work.
deferred-open
Advanced tools
Make methods wait for async constructors.
For example, database libraries like LevelUp or node-redis expose an API that you can use immediately, although the connection hasn't been established yet. This greatly helps reduce callback depth.
In most cases you rather want this:
var db = levelup('/db');
db.get('foo', function (err, val) {
val == 'bar';
});
than this:
levelup('/db', function (err, db) {
if (err) throw err;
db.get('foo', function (err, val) {
val == 'bar';
})
});
So, all calls to member functions have to be deffered until the connection is up. This module helps by mixing in to an Object and deferring operations until the constructor is done doing its async stuff.
Install Deferred onto a function and make doAThing wait until the
constructor finished doing its async operations.
var Deferred = require('deferred-open');
function Something () {
var self = this;
Deferred.install(self); // => self._ready, self._queue
self.loaded = false; // fake internal state
setTimeout(function () { // some async function
self.loaded = true;
self._ready();
}, 500);
}
Something.prototype.doAThing = Deferred(function () {
if (!this.loaded) throw new Error('oops');
console.log('success');
});
And use it like this:
var something = new Something();
something.doAThing(); // "success" after 500ms
If you want to support non deferred open too, use this._queue:
function Something (cb) {
var self = this;
if (!(self instanceof Something)) return new Something(cb);
Deferred.install(self);
if (cb) self._queue(cb); // allow async api too
self.loaded = false;
setTimeout(function () {
self.loaded = true;
self._ready();
}, 500);
}
Now you can use it like this:
// Deferred
var something = new Something();
something.doAThing();
// Or non deferred
Something(function (err, something) {
something.doAThing();
});
Call this in fn's constructor in order to install the _ready and _queue
member functions.
Call this when you finished doing your async stuff.
If you pass something as the err parameter,
Call fn with (err, this) as soon as this._ready() has been
called (with the optional error argument).
Returns a function that gets called only after the internal deferred object
has been signaled by this._ready(). Returns this when not ready, so it
works with chained/fluent apis.
Returns a function the gets called only after the constructor is ready, but returns a paused stream immediately, that will later be hooked up with the real stream.
opts is an optional options object that supports
readable (Boolean): The returned stream should be readable. Default: true.writable (Boolean): The returned stream should be writable. Default: true.With npm do
$ npm install deferred-open
(MIT)
Copyright (c) 2013 Julian Gruber <julian@juliangruber.com>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
FAQs
Make methods wait for async constructors.
We found that deferred-open 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.

Security News
An AI agent is merging PRs into major OSS projects and cold-emailing maintainers to drum up more work.

Research
/Security News
Chrome extension CL Suite by @CLMasters neutralizes 2FA for Facebook and Meta Business accounts while exfiltrating Business Manager contact and analytics data.

Security News
After Matplotlib rejected an AI-written PR, the agent fired back with a blog post, igniting debate over AI contributions and maintainer burden.