# promise-object

> promise-object wrapper for any type of promise

Latest version **0.1.7** (published 2016-04-12) · MIT license · 0 weekly downloads

## Install

```sh
npm install promise-object
pnpm add promise-object
yarn add promise-object
bun add promise-object
```

## 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.1.7 |
| Published | 2016-04-12 |
| First published | 2013-01-31 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 16 |
| Author | Chad Scira |
| Maintainers | icodeforlove |
| Keywords | promises, helper, prototype |

## Links

- npm: https://www.npmjs.com/package/promise-object
- Repository: https://github.com/icodeforlove/node-promise-object
- Homepage: https://github.com/icodeforlove/node-promise-object#readme
- Issues: https://github.com/icodeforlove/node-promise-object/issues
- npm.io page: https://npm.io/package/promise-object

## Alternatives

- [lodash.assign](https://npm.io/package/lodash.assign.md) — 2.3M weekly downloads
- [lodash.chunk](https://npm.io/package/lodash.chunk.md) — 1.8M weekly downloads
- [react-native-ios-utilities](https://npm.io/package/react-native-ios-utilities.md) — 138.5K weekly downloads
- [@technically/lodash](https://npm.io/package/@technically/lodash.md) — 50.9K weekly downloads
- [@fluid-topics/ft-icon](https://npm.io/package/@fluid-topics/ft-icon.md) — 20.6K weekly downloads

## Recent versions

- 0.1.7 (latest) — 2016-04-12
- 0.1.6 — 2016-01-04
- 0.1.5 — 2015-01-05
- 0.1.4 — 2014-10-02
- 0.1.3 — 2014-08-20
- 0.1.2 — 2014-04-07
- 0.1.1 — 2014-01-20
- 0.1.0 — 2014-01-12
- 0.0.22 — 2014-01-12
- 0.0.21 — 2014-01-12
- 0.0.20 — 2013-07-29
- 0.0.19 — 2013-04-17
- 0.0.18 — 2013-02-25
- 0.0.17 — 2013-02-20
- 0.0.16 — 2013-02-18
- … 15 more at https://npm.io/package/promise-object/versions

## README

# THIS IS DEPRECATED IN FAVOR OF ES7 ASYNC/AWAIT

## promise-object [![Build Status](https://travis-ci.org/icodeforlove/node-promise-object.png?branch=master)](https://travis-ci.org/icodeforlove/node-promise-object)
provides a base object that gives you the ability to create promise methods just by setting the first parameter to $deferred and also binds those methods to the instance. It also allows you to extend any method and use mixins.

promise-object is very tiny - 1.3KB gzipped (3.1KB not gzipped)

## installation
	npm install promise-object

## pseudo params
there are a few rules with these params
* if you want to use **$deferred** it MUST be the first param
* any other pseudo param must be before any real params

these pseudo params are supported
* **$deferred** converts the method into a deferred method
* **$super** returns the parent method
* **$class** returns the class
* **$self** alternative to var self = this;
* **$config** ensures that the first argument is an object

## $config
helper that makes working with $config objects a little easier

```javascript
var Promise = require('bluebird'),
	PromiseObject = require('promise-object')(Promise);

var User = PromiseObject.create({
	initialize: function ($config) {
		this._name = $config.name;
	}
});

new User({name: 'joe'});
new User(); // this does not error out because $config was replaced with an empty object
```

## class / instance methods
you can specify class methods by placing a $ in front of the method like this

```javascript
var Class = PromiseObject.create({
	initialize: function ($class) {
		$class.method(); // returns 'class method'

		this.method(); // returns 'instance method'
	},

	$method: function () {
		return 'class method';
	},

	method: function () {
		return 'instance method';
	}
});
```

this would allow you to call the class method via `Class.method`

## $deferred / promises
promoise-object is promise library agnostic, you initialize the wrapper by passing in the promise library you are using.

below is an example of using promises and showing errors

```javascript
var Promise = require('bluebird'),
	PromiseObject = require('promise-object')(Promise);

var User = PromiseObject.create({
	initialize: function (name) {
		this._name = name;
	},

	getInfo: function ($deferred, error) {
		setTimeout(function () {
			if (error) {
				$deferred.reject(new Error('Something went wrong'));
			} else {
				$deferred.resolve({age: 12});
			}
		}, 1000);
	}
});

var joe = new User('joe');
joe.getInfo(false).then(
	function (info) {
		console.log(info);
	},
	function (error) {
		console.log(error);
	}
);
```

## deferred generators

if you are using a promise library that has `coroutine` support (like bluebird) you can do the following

```javascript
getInfo: function *($deferred) {
	var one = yield this.getSomething();
	$deferred.resolve(one);
}
```

## extending
any method can be extended upon, **$super** is used to request the parent method
```javascript
var Promise = require('bluebird'),
	PromiseObject = require('promise-object')(Promise);

var User = PromiseObject.create({
	initialize: function (name) {
		this._name = name;
	},

	getInfo: function ($deferred) {
		setTimeout(function () {
			$deferred.resolve({age: 12});
		}, 0);
	}
});

var Admin = User.extend({
	initialize: function ($super, name) {
		$super(name);
	},

	getInfo: function ($deferred, $super) {
		$super().then(function (info) {
			info.moreStuff = 123;

			$deferred.resolve(info);

		}, $deferred.reject);
	}
});

var joe = new Admin('joe');
joe.getInfo().then(function (info) {
	console.log(info);
});
```

## reopen
you can add methods to an instance by passing them via `.reopen` like this

```javascript
var user = new User();
user.reopen({
	getName: function ($deferred, $self) {
		setTimeout(function () {
			$deferred.resolve($self._name);
		}, 1000);
	}
});
```

and you can add methods to a class like this

```javascript
User.reopen({
	getName: function ($deferred, $self) {
		setTimeout(function () {
			$deferred.resolve($self._name);
		}, 1000);
	}
});
```

when you reopen a method that already exists you gain access to `$super`

## mixins
```javascript
var Promise = require('bluebird'),
	PromiseObject = require('promise-object')(Promise);

var Mixin =  {
	getRandomNumber: function () {
		return Math.random();
	}
};

var Mixin2 = {
	getRandomNumberDeferred: function ($deferred) {
		$deferred.resolve(Math.random());
	}
};

var Class = PromiseObject.create(Mixin, Mixin2, {
	initialize: function () {
	}
});

// examples
var example = new Class();

console.log(example.getRandomNumber());

example.getRandomNumberDeferred().then(function (number) {
	console.log(number);
});
```

mixins should only use initialize to store instance vars

```javascript
var Mixin =  {
	initialize: function () {
		this._tags = [];
	},

	hasTag: function (tag) {
		return this._tags.indexOf(tag) !== -1;
	},

	addTag: function (tag) {
		if (this.hasTag(tag)) return;

		this._tags.push(tag);
	}
};
```

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