Skip to content

Commit

Permalink
Merge pull request #85 from then/domains-etc
Browse files Browse the repository at this point in the history
Add builds for domains and setImmedaite
  • Loading branch information
ForbesLindesay committed Apr 7, 2015
2 parents 5f214e1 + b8ac76b commit 0f7bcb9
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 10 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ components
build
node_modules
/lib
/domains
/setimmediate
22 changes: 21 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Note that the [es5-shim](https://github.com/es-shims/es5-shim) must be loaded be

## Usage

The example below shows how you can load the promise library (in a way that works on both client and server). It then demonstrates creating a promise from scratch. You simply call `new Promise(fn)`. There is a complete specification for what is returned by this method in [Promises/A+](http://promises-aplus.github.com/promises-spec/).
The example below shows how you can load the promise library (in a way that works on both client and server using node or browserify). It then demonstrates creating a promise from scratch. You simply call `new Promise(fn)`. There is a complete specification for what is returned by this method in [Promises/A+](http://promises-aplus.github.com/promises-spec/).

```javascript
var Promise = require('promise');
Expand All @@ -56,6 +56,26 @@ var promise = new Promise(function (resolve, reject) {
});
```

If you need [domains](https://iojs.org/api/domain.html) support, you should instead use:

```js
var Promise = require('promise/domains');
```

If you are in an environment that implements `setImmediate` and don't want the optimisations provided by asap, you can use:

```js
var Promise = require('promise/setimmediate');
```

If you only want part of the features, e.g. just a pure ES6 polyfill:

```js
var Promise = require('promise/lib/es6-extensions');
// or require('promise/domains/es6-extensions');
// or require('promise/setimmediate/es6-extensions');
```

## API

Before all examples, you will need:
Expand Down
19 changes: 19 additions & 0 deletions build.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,22 @@ fs.readdirSync(__dirname + '/src').forEach(function (filename) {
var out = fixup(src);
fs.writeFileSync(__dirname + '/lib/' + filename, out);
});

rimraf.sync(__dirname + '/domains/');
fs.mkdirSync(__dirname + '/domains/');
fs.readdirSync(__dirname + '/src').forEach(function (filename) {
var src = fs.readFileSync(__dirname + '/src/' + filename, 'utf8');
var out = fixup(src);
out = out.replace(/require\(\'asap\/raw\'\)/g, "require('asap')");
fs.writeFileSync(__dirname + '/domains/' + filename, out);
});

rimraf.sync(__dirname + '/setimmediate/');
fs.mkdirSync(__dirname + '/setimmediate/');
fs.readdirSync(__dirname + '/src').forEach(function (filename) {
var src = fs.readFileSync(__dirname + '/src/' + filename, 'utf8');
var out = fixup(src);
out = out.replace(/var asap \= require\(\'([a-z\/]+)\'\)/g, '');
out = out.replace(/asap/g, "setImmediate");
fs.writeFileSync(__dirname + '/setimmediate/' + filename, out);
});
6 changes: 1 addition & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
'use strict';

module.exports = require('./lib/core.js')
require('./lib/done.js')
require('./lib/finally.js')
require('./lib/es6-extensions.js')
require('./lib/node-extensions.js')
module.exports = require('./lib')
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"description": "Bare bones Promises/A+ implementation",
"main": "index.js",
"scripts": {
"prepublish": "node build",
"pretest": "node build",
"pretest-resolve": "node build",
"pretest-extensions": "node build",
Expand Down
5 changes: 2 additions & 3 deletions src/done.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
'use strict';

var Promise = require('./core.js')
var asap = require('asap')

module.exports = Promise
Promise.prototype.done = function (onFulfilled, onRejected) {
var self = arguments.length ? this.then.apply(this, arguments) : this
self.then(null, function (err) {
asap(function () {
setTimeout(function () {
throw err
})
}, 0)
})
}
2 changes: 1 addition & 1 deletion src/es6-extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//This file contains the ES6 extensions to the core Promises/A+ API

var Promise = require('./core.js')
var asap = require('asap')
var asap = require('asap/raw')

module.exports = Promise

Expand Down
7 changes: 7 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

module.exports = require('./core.js')
require('./done.js')
require('./finally.js')
require('./es6-extensions.js')
require('./node-extensions.js')

0 comments on commit 0f7bcb9

Please sign in to comment.