# async-resource

> gives you a simple interface for initializing async resources. send it an init function and it will return a function that calls back with your initialized resource.

Latest version **0.0.9** (published 2015-02-19) · BSD license · 0 weekly downloads

## Install

```sh
npm install async-resource
pnpm add async-resource
yarn add async-resource
bun add async-resource
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.9 |
| Published | 2015-02-19 |
| First published | 2012-12-14 |
| Weekly downloads | 0 |
| License | BSD |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 (+2 in 1 direct dependencies) |
| Install scripts | no |
| Author | Dan McAulay |
| Maintainers | dmcaulay |

## Links

- npm: https://www.npmjs.com/package/async-resource
- npm.io page: https://npm.io/package/async-resource

## Dependencies (1)

- [underscore](https://npm.io/package/underscore.md) ~1.6.0

## Recent versions

- 0.0.9 (latest) — 2015-02-19
- 0.0.8 — 2014-05-02
- 0.0.7 — 2014-02-24
- 0.0.6 — 2014-02-19
- 0.0.5 — 2014-02-19
- 0.0.4 — 2013-04-12
- 0.0.3 — 2013-04-09
- 0.0.2 — 2013-04-09
- 0.0.1 — 2012-12-18
- 0.0.0 — 2012-12-14

## README

# async-resource

This is a simple interface for initializing async resources. Send it an init function and it will return a function that calls back with your initialized resource. It ensures that the resource will only be initialized once.

## Installation

```bash
$ npm install async-resource
```

## Usage

With a class.

``` js
// DB client has a bunch of setup that requires async calls
var DBClient = require('db').Client;


var getResource = require('async-resource').get
var ensureResource = require('async-resource').ensure

function DbWrapper(connectionUrl, config) {
  var self = this;

  // wrap the init method with getResource
  this.ensureConnected = getResource(function(callback) {
    DbClient.connect(connectionUrl, function(err, db) {
      if (err) return callback(err)
      self.db = db;

      // make sure to callback with true if the setup is successful
      if (!config.indexes) return callback(null, true);
      setupIndexes(db, config.indexes, function(err) {
        callback(err, true);
      });
    });
  })
};

// simple methods that depend on a connected db.
// a real wrapper would most likely have more complicted
// methods.
DbWrapper.prototype.select = function(query, callback) {
  this.db.select(query, callback);
};

DbWrapper.prototype.insert = function(query, callback) {
  this.db.insert(query, callback);
};

DbWrapper.prototype.update = function(query, callback) {
  this.db.update(query, callback);
};

DbWrapper.prototype.delete = function(query, callback) {
  this.db.delete(query, callback);
};

// wrap all the methods that depend on the connected DB
ensureResource(DbWrapper.prototype, 'ensureConnected', [ 'select', 'insert', 'update', 'delete' ]);
```

With a function.

``` js
// DB client has a bunch of setup that requires async calls
var DBClient = require('db').Client;


var getResource = require('async-resource').get
var ensureFn = require('async-resource').ensureFn

var db;

var init = function(callback) {
  DbClient.connect(connectionUrl, function(err, db) {
    if (err) return callback(err)
    db = db;

    // make sure to callback with true if the setup is successful
    if (!config.indexes) return callback(null, true);
    setupIndexes(db, config.indexes, function(err) {
      callback(err, true);
    });
  });
};
var ensureInit = getResource(init);

var select = function(query, callback) {
  db.select(query, callback);
};
// wrap select
select = ensureFn(select, ensureInit);
```

---
_Source: https://npm.io/package/async-resource · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
