Skip to content
This repository has been archived by the owner on Apr 16, 2019. It is now read-only.

Commit

Permalink
Merge pull request #18 from juandopazo/cycle
Browse files Browse the repository at this point in the history
Better matching the ES6 spec
  • Loading branch information
juandopazo committed Nov 25, 2014
2 parents c210eba + 20ba926 commit 5986fd8
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 40 deletions.
13 changes: 6 additions & 7 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "ypromise",
"version": "0.2.4",
"version": "0.3.0",
"description": "ES6 Promise polyfill",
"author": "Juan Dopazo <dopazo.juan@gmail.com>",
"licenses": [
Expand All @@ -26,13 +26,12 @@
],
"main": "promise.js",
"devDependencies": {
"promises-aplus-tests": "2.0.3",
"promises-aplus-tests": "2.1.0",
"requirejs": "~2.1.9",
"mocha": "~1.7.1",
"expect.js": "~0.2.0",
"sinon": "~1.8.2",
"istanbul": "~0.2.4",
"xunit-file": "*",
"mocha": "~2.0.1",
"expect.js": "~0.3.1",
"sinon": "~1.11.1",
"istanbul": "~0.3.2",
"yui-lint": "^0.1.4",
"jshint": "^2.4.4"
},
Expand Down
33 changes: 6 additions & 27 deletions promise.js
Expand Up @@ -62,11 +62,13 @@ http://yuilibrary.com/license/
**/
function Promise(fn) {
if (!(this instanceof Promise)) {
Promise._log('Promises should always be created with new Promise(). This will throw an error in the future', 'error');
return new Promise(fn);
throw new TypeError(this + 'is not a promise');
}
if (typeof fn !== 'function') {
throw new TypeError('Promise resolver ' + fn + ' is not a function');
}

var resolver = new Promise.Resolver(this);
var resolver = new Resolver();

/**
A reference to the resolver object that handles this promise
Expand Down Expand Up @@ -183,18 +185,6 @@ http://yuilibrary.com/license/
};
};

/**
Logs a message. This method is designed to be overwritten with YUI's `log`
function.
@method _log
@param {String} msg Message to log
@param {String} type Log level. One of 'error', 'warn', 'info'.
@static
@private
**/
Promise._log = function (msg, type) { /* istanbul ignore else */ if (typeof console !== 'undefined') { console[type](msg); } };

/*
Ensures that a certain value is a promise. If it is not a promise, it wraps it
in one.
Expand Down Expand Up @@ -338,9 +328,8 @@ http://yuilibrary.com/license/
@class Promise.Resolver
@constructor
@param {Promise} promise The promise instance this resolver will be handling
**/
function Resolver(promise) {
function Resolver() {
/**
List of success callbacks
Expand All @@ -359,15 +348,6 @@ http://yuilibrary.com/license/
**/
this._errbacks = [];

/**
The promise for this Resolver.
@property promise
@type Promise
@deprecated
**/
this.promise = promise;

/**
The status of the operation. This property may take only one of the following
values: 'pending', 'fulfilled' or 'rejected'.
Expand Down Expand Up @@ -439,7 +419,6 @@ http://yuilibrary.com/license/
if (status === 'pending' || status === 'accepted') {
this._result = reason;
this._status = 'rejected';
if (!this._errbacks.length) {Promise._log('Promise rejected but no error handlers were registered to it', 'info');}
}

if (this._status === 'rejected') {
Expand Down
34 changes: 28 additions & 6 deletions tests/unit/assets/promise-tests.js
Expand Up @@ -3,13 +3,14 @@ Copyright 2013 Yahoo! Inc. All rights reserved.
Licensed under the BSD License.
http://yuilibrary.com/license/
*/
/*global describe, specify, it, expect, Promise*/
var dummy = {dummy: 'dummy'};

function isFulfilled(promise, done, callback) {
promise.then(function (value) {
setTimeout(function () {
callback(value);
}, 0)
}, 0);
}, function () {
setTimeout(function () {
done(new Error('Promise rejected instead of fulfilled'));
Expand Down Expand Up @@ -46,21 +47,23 @@ function rejectedAfter(ms) {
}

function extend(subclass, superclass, proto, attrs) {
var prop;

extend.f.prototype = superclass.prototype;
subclass.prototype = new extend.f();
subclass.prototype.constructor = subclass;
subclass.superclass = superclass.prototype;

if (proto) {
for (var prop in proto) {
for (prop in proto) {
if (proto.hasOwnProperty(prop)) {
subclass.prototype[prop] = proto[prop];
}
}
}

if (attrs) {
for (var prop in attrs) {
for (prop in attrs) {
if (attrs.hasOwnProperty(prop)) {
subclass[prop] = attrs[prop];
}
Expand All @@ -73,8 +76,25 @@ extend.f = function () {};

describe('Basic promise behavior', function () {
describe('Promise constructor', function () {
it('should return a promise when used as a function', function () {
expect(Promise(function () {})).to.be.a(Promise);
it('throws when used as a function', function () {
function testFn() {
/*jshint newcap: false*/
return Promise(function () {});
/*jshint newcap: true*/
}
expect(testFn).to.throwException(function (err) {
expect(err).to.be.a(TypeError);
});
});

it('throws when not passed a function', function () {
function testFn() {
return new Promise(5);
}

expect(testFn).to.throwException(function (err) {
expect(err).to.be.a(TypeError);
});
});

specify('fulfilling more than once should not change the promise value', function (done) {
Expand Down Expand Up @@ -111,7 +131,7 @@ describe('Basic promise behavior', function () {
describe('promise.then()', function () {
it('returns a promise', function () {
var promise = new Promise(function (resolve) {
resolve(5)
resolve(5);
});
expect(promise).to.be.a(Promise);
});
Expand Down Expand Up @@ -141,7 +161,9 @@ describe('Basic promise behavior', function () {
});

describe('Behavior of the then() callbacks', function () {
/*jshint newcap: false, evil: true*/
var global = Function('return this')();
/*jshint newcap: true, evil: false*/

specify('throwing inside a callback should turn into a rejection', function (done) {
var error = new Error('Arbitrary error');
Expand Down

0 comments on commit 5986fd8

Please sign in to comment.