Commit 8c80ba5e authored by Stefan Penner's avatar Stefan Penner

add RSVP.reject

parent 84469f63
define(
["rsvp/events","rsvp/promise","rsvp/node","rsvp/all","rsvp/hash","rsvp/defer","rsvp/config","rsvp/resolve","exports"],
function(__dependency1__, __dependency2__, __dependency3__, __dependency4__, __dependency5__, __dependency6__, __dependency7__, __dependency8__, __exports__) {
["rsvp/events","rsvp/promise","rsvp/node","rsvp/all","rsvp/hash","rsvp/defer","rsvp/config","rsvp/resolve","rsvp/reject","exports"],
function(__dependency1__, __dependency2__, __dependency3__, __dependency4__, __dependency5__, __dependency6__, __dependency7__, __dependency8__, __dependency9__, __exports__) {
"use strict";
var EventTarget = __dependency1__.EventTarget;
var Promise = __dependency2__.Promise;
......@@ -10,6 +10,7 @@ define(
var defer = __dependency6__.defer;
var config = __dependency7__.config;
var resolve = __dependency8__.resolve;
var reject = __dependency9__.reject;
function configure(name, value) {
config[name] = value;
......@@ -24,4 +25,5 @@ define(
__exports__.denodeify = denodeify;
__exports__.configure = configure;
__exports__.resolve = resolve;
__exports__.reject = reject;
});
This diff is collapsed.
This diff is collapsed.
......@@ -45,7 +45,11 @@ define(
this.trigger('error', { detail: event.detail });
}, this);
resolver(resolvePromise, rejectPromise);
try {
resolver(resolvePromise, rejectPromise);
} catch(e) {
rejectPromise(e);
}
};
var invokeCallback = function(type, promise, callback, event) {
......@@ -65,12 +69,8 @@ define(
succeeded = true;
}
if (objectOrFunction(value) && isFunction(value.then)) {
value.then(function(value) {
resolve(promise, value);
}, function(error) {
reject(promise, error);
});
if (handleThenable(promise, value)) {
return;
} else if (hasCallback && succeeded) {
resolve(promise, value);
} else if (failed) {
......@@ -115,24 +115,45 @@ define(
EventTarget.mixin(Promise.prototype);
function resolve(promise, value) {
if (promise === value) {
fulfill(promise, value);
} else if (objectOrFunction(value) && isFunction(value.then)) {
value.then(function(val) {
if (value !== val) {
resolve(promise, val);
} else {
fulfill(promise, val);
}
}, function(val) {
reject(promise, val);
});
} else {
} else if (!handleThenable(promise, value)) {
fulfill(promise, value);
}
}
function handleThenable(promise, value) {
var then = null;
if (objectOrFunction(value)) {
try {
then = value.then;
} catch(e) {
reject(promise, e);
return true;
}
if (isFunction(then)) {
try {
then.call(value, function(val) {
if (value !== val) {
resolve(promise, val);
} else {
fulfill(promise, val);
}
}, function(val) {
reject(promise, val);
});
} catch (e) {
reject(promise, e);
}
return true;
}
}
return false;
}
function fulfill(promise, value) {
config.async(function() {
promise.trigger('promise:resolved', { detail: value });
......
define(
["rsvp/promise","exports"],
function(__dependency1__, __exports__) {
"use strict";
var Promise = __dependency1__.Promise;
function objectOrFunction(x) {
return typeof x === "function" || (typeof x === "object" && x !== null);
}
function reject(reason) {
return new Promise(function (resolve, reject) {
reject(reason);
});
}
__exports__.reject = reject;
});
......@@ -6,9 +6,10 @@ import { hash } from "rsvp/hash";
import { defer } from "rsvp/defer";
import { config } from "rsvp/config";
import { resolve } from "rsvp/resolve";
import { reject } from "rsvp/reject";
function configure(name, value) {
config[name] = value;
}
export { Promise, EventTarget, all, hash, defer, denodeify, configure, resolve };
export { Promise, EventTarget, all, hash, defer, denodeify, configure, resolve, reject };
import { Promise } from "rsvp/promise";
function objectOrFunction(x) {
return typeof x === "function" || (typeof x === "object" && x !== null);
}
function reject(reason) {
return new Promise(function (resolve, reject) {
reject(reason);
});
}
export { reject };
......@@ -574,6 +574,23 @@ describe("RSVP extensions", function() {
});
});
describe("RSVP.reject", function(){
specify("it should exist", function(){
assert(RSVP.reject);
});
describe('it rejects', function(){
var reason = 'the reason',
promise = RSVP.reject(reason);
promise.then(function(){
assert(false, 'should not fulfill');
}, function(actualReason){
assert.equal(reason, actualReason);
});
});
});
describe("RSVP.resolve", function(){
specify("it should exist", function(){
assert(RSVP.resolve);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment