Skip to content

Commit

Permalink
Implement resolvers strawman/D
Browse files Browse the repository at this point in the history
  • Loading branch information
ForbesLindesay committed Mar 5, 2013
1 parent b1d7b7e commit 7dd7c9f
Show file tree
Hide file tree
Showing 6 changed files with 374 additions and 117 deletions.
37 changes: 28 additions & 9 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,32 @@

## Installation

Client:

$ component install then/promise

Server:

$ npm install promise

Client:

$ component install then/promise

## API

In 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 resolver object has two methods `reject` and `fulfill` and their use is demonstrated here:
In 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/).

```javascript
var Promise = require('promise');

var promise = new Promise(function (resolver) {
var promise = new Promise(function (resolve, reject) {
get('http://www.google.com', function (err, res) {
if (err) resolver.reject(err);
else resolver.fulfill(res);
if (err) reject(err);
else resolve(res);
});
});
```

## Extending Promises

There are two options for extending the promises created by this library.
There are three options for extending the promises created by this library.

### Inheritance

Expand All @@ -42,6 +42,7 @@ var promise = new Promise(function (resolver) {
```javascript
var Promise = require('promise');
function Awesome(fn) {
if (!(this instanceof Awesome)) return new Awesome(fn);
Promise.call(this, fn);
}
Awesome.prototype = Object.create(Promise.prototype);
Expand All @@ -57,6 +58,24 @@ Awesome.prototype.spread = function (cb) {

N.B. if you fail to set the prototype and constructor properly or fail to do Promise.call, things can fail in really subtle ways.

### Wrap

This is the nuclear option, for when you want to start from scratch. It ensures you won't be impacted by anyone who is extending the prototype (see below).

```javascript
function Uber(fn) {
if (!(this instanceof Uber)) return new Uber(fn);
var _prom = new Promise(fn);
this.then = _prom.then;
}

Uber.prototype.spread = function (cb) {
return this.then(function (arr) {
return cb.apply(this, arr);
})
};
```

### Extending the Prototype

In general, you should never extend the prototype of this promise implimenation because your extensions could easily conflict with someone elses extensions. However, this organisation will host a library of extensions which do not conflict with each other, so you can safely enable any of those. If you think of an extension that we don't provide and you want to write it, submit an issue on this repository and (if I agree) I'll set you up with a repository and give you permission to commit to it.
Expand Down
262 changes: 164 additions & 98 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,98 +1,164 @@
var isPromise = require('is-promise')

var nextTick;
if (typeof setImediate === 'function') nextTick = setImediate
else if (typeof process === 'object' && process && process.nextTick) nextTick = process.nextTick
else nextTick = function (cb) { setTimeout(cb, 0) }

var extensions = [];

module.exports = Promise
function Promise(fn) {
if (!(this instanceof Promise)) {
return typeof fn === 'function' ? new Promise(fn) : defer()
}
var isResolved = false
var isFulfilled = false
var value
var waiting = []
var running = false

function next(skipTimeout) {
if (waiting.length) {
running = true
waiting.shift()(skipTimeout || false)
} else {
running = false
}
}
this.then = then;
function then(cb, eb) {
return new Promise(function (resolver) {
function done(skipTimeout) {
var callback = isFulfilled ? cb : eb
if (typeof callback === 'function') {
function timeoutDone() {
var val;
try {
val = callback(value)
} catch (ex) {
resolver.reject(ex)
return next()
}
resolver.fulfill(val);
next(true);
}
if (skipTimeout) timeoutDone()
else nextTick(timeoutDone)
} else if (isFulfilled) {
resolver.fulfill(value)
next(skipTimeout)
} else {
resolver.reject(value)
next(skipTimeout)
}
}
waiting.push(done)
if (isResolved && !running) next()
});
}

(function () {
function fulfill(val) {
if (isResolved) return
if (isPromise(val)) val.then(fulfill, reject)
else {
isResolved = isFulfilled = true
value = val
next()
}
}
function reject(err) {
if (isResolved) return
isResolved = true
isFulfilled = false
value = err
next()
}
var resolver = {fulfill: fulfill, reject: reject};
for (var i = 0; i < extensions.length; i++) {
extensions[i](this, resolver);
}
if (typeof fn === 'function') {
try {
fn(resolver)
} catch (ex) {
resolver.reject(ex);
}
}
}());
}
function defer() {
var resolver
var promise = new Promise(function (res) { resolver = res })
return {resolver: resolver, promise: promise}
}
Promise.use = function (extension) {
extensions.push(extension);
};
var isPromise = require('is-promise')

var nextTick
if (typeof setImediate === 'function') nextTick = setImediate
else if (typeof process === 'object' && process && process.nextTick) nextTick = process.nextTick
else nextTick = function (cb) { setTimeout(cb, 0) }

var extensions = []

module.exports = Promise
function Promise(fn) {
if (!(this instanceof Promise)) {
return fn ? new Promise(fn) : defer()
}
if (typeof fn !== 'function') {
throw new TypeError('fn is not a function')
}

var state = {
isResolved: false,
isSettled: false,
isFulfilled: false,
value: null,
waiting: [],
running: false
}

function _resolve(val) {
resolve(state, val);
}
function _reject(err) {
reject(state, err);
}
this.then = function _then(onFulfilled, onRejected) {
return then(state, onFulfilled, onRejected);
}

_resolve.fulfill = deprecate(_resolve, 'resolver.fulfill(x)', 'resolve(x)')
_resolve.reject = deprecate(_reject, 'resolver.reject', 'reject(x)')

try {
fn(_resolve, _reject)
} catch (ex) {
_reject(ex)
}
}

function resolve(promiseState, value) {
if (promiseState.isResolved) return
if (isPromise(value)) {
assimilate(promiseState, value)
} else {
settle(promiseState, true, value)
}
}

function reject(promiseState, reason) {
if (promiseState.isResolved) return
settle(promiseState, false, reason)
}

function then(promiseState, onFulfilled, onRejected) {
return new Promise(function (resolve, reject) {
function done(next, skipTimeout) {
var callback = promiseState.isFulfilled ? onFulfilled : onRejected
if (typeof callback === 'function') {
function timeoutDone() {
var val
try {
val = callback(promiseState.value)
} catch (ex) {
reject(ex)
return next(true)
}
resolve(val)
next(true)
}
if (skipTimeout) timeoutDone()
else nextTick(timeoutDone)
} else if (promiseState.isFulfilled) {
resolve(promiseState.value)
next(skipTimeout)
} else {
reject(promiseState.value)
next(skipTimeout)
}
}
promiseState.waiting.push(done)
if (promiseState.isSettled && !promiseState.running) processQueue(promiseState)
})
}

function processQueue(promiseState) {
function next(skipTimeout) {
if (promiseState.waiting.length) {
promiseState.running = true
promiseState.waiting.shift()(next, skipTimeout)
} else {
promiseState.running = false
}
}
next(false)
}

function settle(promiseState, isFulfilled, value) {
if (promiseState.isSettled) return

promiseState.isResolved = promiseState.isSettled = true
promiseState.value = value
promiseState.isFulfilled = isFulfilled

processQueue(promiseState)
}

function assimilate(promiseState, thenable) {
try {
promiseState.isResolved = true
thenable.then(function (res) {
if (isPromise(res)) {
assimilate(promiseState, res)
} else {
settle(promiseState, true, res)
}
}, function (err) {
settle(promiseState, false, err)
})
} catch (ex) {
settle(promiseState, false, ex)
}
}

Promise.use = function (extension) {
extensions.push(extension)
}


function deprecate(method, name, alternative) {
return function () {
var err = new Error(name + ' is deprecated use ' + alternative)
if (typeof console !== 'undefined' && console && typeof console.warn === 'function') {
console.warn(name + ' is deprecated use ' + alternative)
if (err.stack) console.warn(err.stack)
} else {
nextTick(function () {
throw err
})
}
method.apply(this, arguments)
}
}
function defer() {
var err = new Error('promise.defer() is deprecated')
if (typeof console !== 'undefined' && console && typeof console.warn === 'function') {
console.warn('promise.defer() is deprecated')
if (err.stack) console.warn(err.stack)
} else {
nextTick(function () {
throw err
})
}
var resolver
var promise = new Promise(function (res) { resolver = res })
return {resolver: resolver, promise: promise}
}
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "Bare bones Promises/A+ implementation",
"main": "index.js",
"scripts": {
"test": "promises-aplus-tests test/adapter-a.js"
"test": "mocha -R spec --timeout 200 --slow 99999"
},
"repository": {
"type": "git",
Expand All @@ -16,6 +16,8 @@
"is-promise": "~1"
},
"devDependencies": {
"promises-aplus-tests": "*"
"promises-aplus-tests": "*",
"mocha-as-promised": "~1.2.1",
"better-assert": "~1.0.0"
}
}
}
18 changes: 11 additions & 7 deletions test/adapter-a.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
var Promise = require('../');

module.exports = {
pending: function () {
var resolver;
var promise = new Promise(function (res) { resolver = res; });
var resolve, reject;
var promise = new Promise(function (_resolve, _reject) {
resolve = _resolve;
reject = _reject;
});
return {
promise: promise,
fulfill: resolver.fulfill,
reject: resolver.reject
fulfill: resolve,
reject: reject
};
},
fulfilled: function (value) {
return new Promise(function (res) { res.fulfill(value); });
return new Promise(function (resolve) { resolve(value); });
},
rejected: function (value) {
return new Promise(function (res) { res.reject(value); });
return new Promise(function (resolve, reject) { reject(value); });
}
}
}
Loading

0 comments on commit 7dd7c9f

Please sign in to comment.