
Company News
Socket Joins the OpenJS Foundation
Socket is proud to join the OpenJS Foundation as a Silver Member, deepening our commitment to the long-term health and security of the JavaScript ecosystem.
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 this internal deffered object
has been signaled by this._resolve().
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.
The npm package deferred-open receives a total of 10 weekly downloads. As such, deferred-open popularity was classified as not popular.
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.

Company News
Socket is proud to join the OpenJS Foundation as a Silver Member, deepening our commitment to the long-term health and security of the JavaScript ecosystem.

Security News
npm now links to Socket's security analysis on every package page. Here's what you'll find when you click through.

Security News
A compromised npm publish token was used to push a malicious postinstall script in cline@2.3.0, affecting the popular AI coding agent CLI with 90k weekly downloads.