├── .gitignore ├── .npmignore ├── CHANGELOG.md ├── LICENSE ├── Makefile ├── README.md ├── index.js ├── package.json └── test ├── index_test.js └── mocha.opts /.gitignore: -------------------------------------------------------------------------------- 1 | /node_modules/ 2 | /*.tgz 3 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | /*.tgz 2 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ## 1.0.0 (Jan 18, 2015) 2 | - As promised. 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | PromiseDefer.js 2 | Copyright (C) 2015– Andri Möll 3 | 4 | This program is free software: you can redistribute it and/or modify it under 5 | the terms of the GNU Affero General Public License as published by the Free 6 | Software Foundation, either version 3 of the License, or any later version. 7 | 8 | Additional permission under the GNU Affero GPL version 3 section 7: 9 | If you modify this Program, or any covered work, by linking or 10 | combining it with other code, such other code is not for that reason 11 | alone subject to any of the requirements of the GNU Affero GPL version 3. 12 | 13 | In summary: 14 | - You can use this program for no cost. 15 | - You can use this program for both personal and commercial reasons. 16 | - You do not have to share your own program's code which uses this program. 17 | - You have to share modifications (e.g bug-fixes) you've made to this program. 18 | 19 | For the full copy of the GNU Affero General Public License see: 20 | http://www.gnu.org/licenses. 21 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | NODE_OPTS = 2 | TEST_OPTS = 3 | 4 | love: 5 | @echo "Feel like makin' love." 6 | 7 | test: 8 | @node $(NODE_OPTS) ./node_modules/.bin/_mocha -R dot $(TEST_OPTS) 9 | 10 | spec: 11 | @node $(NODE_OPTS) ./node_modules/.bin/_mocha -R spec $(TEST_OPTS) 12 | 13 | autotest: 14 | @node $(NODE_OPTS) ./node_modules/.bin/_mocha -R dot --watch $(TEST_OPTS) 15 | 16 | autospec: 17 | @node $(NODE_OPTS) ./node_modules/.bin/_mocha -R spec --watch $(TEST_OPTS) 18 | 19 | pack: 20 | @file=$$(npm pack); echo "$$file"; tar tf "$$file" 21 | 22 | publish: 23 | npm publish 24 | 25 | tag: 26 | git tag "v$$(node -e 'console.log(require("./package").version)')" 27 | 28 | clean: 29 | rm -f *.tgz 30 | npm prune --production 31 | 32 | .PHONY: love 33 | .PHONY: test spec autotest autospec 34 | .PHONY: pack publish tag 35 | .PHONY: clean 36 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | PromiseDefer.js 2 | =============== 3 | [![NPM version][npm-badge]](http://badge.fury.io/js/promise-defer) 4 | 5 | **PromiseDefer.js** is a small `Promise.defer` "polyfill" to create 6 | a [`Deferred`][deferred] object you can **later resolve or reject** and get 7 | a `Promise` out of. It used to exist in browsers and JavaScript engines as 8 | `Promise.defer`, but was later deprecated. It's still useful in some cases. 9 | 10 | PromiseDefer.js uses the **native ES6 `Promise` by default**, but you can pass 11 | it another **Promises/A+ compatible** constructor function. 12 | 13 | [npm-badge]: https://img.shields.io/npm/v/promise-defer.svg 14 | [deferred]: https://developer.mozilla.org/en-US/docs/Mozilla/JavaScript_code_modules/Promise.jsm/Deferred 15 | [promisesa+]: https://promisesaplus.com/ 16 | 17 | 18 | Installing 19 | ---------- 20 | ```sh 21 | npm install promise-defer 22 | ``` 23 | 24 | PromiseDefer.js follows [semantic versioning](http://semver.org/), so feel 25 | free to depend on its major version with something like `>= 1.0.0 < 2` 26 | (a.k.a `^1.0.0`). 27 | 28 | 29 | Using 30 | ----- 31 | ```javascript 32 | var defer = require("promise-defer") 33 | 34 | var deferred = defer() 35 | deferred.resolve(42) 36 | deferred.promise // Will eventually be resolved with 42. 37 | 38 | var rejection = defer() 39 | rejection.reject(new Error("I'll have none of that, thank you.")) 40 | rejection.promise // Will eventually be rejected with the error. 41 | ``` 42 | 43 | PromiseDefer.js will use the global ES6 `Promise` constructor by default that's 44 | starting to become available in browsers and JavaScript runtimes (e.g. the V8 in 45 | Node.js v0.11). It doesn't therefore have any outside dependencies. 46 | 47 | ### Using another Promise implementation 48 | If you'd like to use another promise implementation or are running in an 49 | environment that doesn't have the native `Promise`, pass 50 | a [Promises/A+][promisesa+] compatible constructor as the first argument: 51 | 52 | ```javascript 53 | var Bluebird = require("bluebird") 54 | var defer = require("promise-defer").bind(null, Bluebird) 55 | defer().promise // => An instance of Bluebird. 56 | ``` 57 | 58 | ### Deferred class 59 | If you need to programmatically differentiate beteween regular objects and 60 | `Deferred` instances, use the `instanceof` operator: 61 | 62 | ```javascript 63 | var Deferred = require("promise-defer") 64 | Deferred() instanceof Deferred // => true 65 | ``` 66 | 67 | 68 | License 69 | ------- 70 | PromiseDefer.js is released under a *Lesser GNU Affero General Public License*, 71 | which in summary means: 72 | 73 | - You **can** use this program for **no cost**. 74 | - You **can** use this program for **both personal and commercial reasons**. 75 | - You **do not have to share your own program's code** which uses this program. 76 | - You **have to share modifications** (e.g. bug-fixes) you've made to this 77 | program. 78 | 79 | For more convoluted language, see the `LICENSE` file. 80 | 81 | 82 | About 83 | ----- 84 | **[Andri Möll][moll]** typed this and the code. 85 | [Monday Calendar][monday] supported the engineering work. 86 | 87 | If you find PromiseDefer.js needs improving, please don't hesitate to type to me 88 | now at [andri@dot.ee][email] or [create an issue online][issues]. 89 | 90 | [email]: mailto:andri@dot.ee 91 | [issues]: https://github.com/moll/js-promise-defer/issues 92 | [moll]: http://themoll.com 93 | [monday]: https://mondayapp.com 94 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | module.exports = Deferred 2 | Deferred.defer = defer 3 | 4 | function Deferred(Promise) { 5 | if (Promise == null) Promise = global.Promise 6 | if (this instanceof Deferred) return defer(Promise, this) 7 | else return defer(Promise, Object.create(Deferred.prototype)) 8 | } 9 | 10 | function defer(Promise, deferred) { 11 | deferred.promise = new Promise(function(resolve, reject) { 12 | deferred.resolve = resolve 13 | deferred.reject = reject 14 | }) 15 | 16 | return deferred 17 | } 18 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "promise-defer", 3 | "version": "1.0.0", 4 | "description": "Polyfill for Promise.defer. Uses the native ES6 Promise. Supports other Promises/A+ implementations.", 5 | "keywords": [ 6 | "defer", 7 | "deferred", 8 | "promise", 9 | "promises", 10 | "unwrap" 11 | ], 12 | "homepage": "https://github.com/moll/js-promise-defer", 13 | "bugs": "https://github.com/moll/js-promise-defer/issues", 14 | 15 | "author": { 16 | "name": "Andri Möll", 17 | "email": "andri@dot.ee", 18 | "url": "http://themoll.com" 19 | }, 20 | 21 | "repository": { 22 | "type": "git", 23 | "url": "git://github.com/moll/js-promise-defer.git" 24 | }, 25 | 26 | "licenses": [{ 27 | "type": "LAGPL", 28 | "url": "https://github.com/moll/js-promise-defer/blob/master/LICENSE" 29 | }], 30 | 31 | "main": "index.js", 32 | "scripts": {"test": "make test"}, 33 | 34 | "devDependencies": { 35 | "mocha": ">= 1.18.2 < 2", 36 | "must": ">= 0.12.0 < 0.13", 37 | "bluebird": ">= 2.7.1 < 3" 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /test/index_test.js: -------------------------------------------------------------------------------- 1 | var Bluebird = require("bluebird") 2 | var Deferred = require("..") 3 | var defer = Deferred 4 | 5 | describe("defer", function() { 6 | it("must return an instance of Deferred", function() { 7 | defer().must.be.an.instanceof(Deferred) 8 | }) 9 | 10 | describe("resolve", function() { 11 | it("must resolve the promise", function() { 12 | var deferred = defer() 13 | deferred.resolve(42) 14 | return deferred.promise.then(function(value) { value.must.equal(42) }) 15 | }) 16 | 17 | it("must resolve the promise given a Promise constructor", function() { 18 | var deferred = defer(Bluebird) 19 | deferred.resolve(42) 20 | return deferred.promise.then(function(value) { value.must.equal(42) }) 21 | }) 22 | }) 23 | 24 | describe("reject", function() { 25 | it("must reject the promise", function(done) { 26 | var deferred = defer() 27 | deferred.reject(42) 28 | deferred.promise.catch(function(value) { value.must.equal(42); done() }) 29 | }) 30 | 31 | it("must reject the promise given a Promise constructor", function(done) { 32 | var deferred = defer(Bluebird) 33 | deferred.reject(42) 34 | deferred.promise.catch(function(value) { value.must.equal(42); done() }) 35 | }) 36 | }) 37 | 38 | describe("promise", function() { 39 | it("must be an instance of Promise", function() { 40 | defer().promise.must.be.an.instanceof(Promise) 41 | }) 42 | 43 | it("must be an instance of the given Promise constructor", function() { 44 | defer(Bluebird).promise.must.be.an.instanceof(Bluebird) 45 | }) 46 | }) 47 | }) 48 | -------------------------------------------------------------------------------- /test/mocha.opts: -------------------------------------------------------------------------------- 1 | --recursive 2 | --check-leaks 3 | --require must 4 | --------------------------------------------------------------------------------