Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added synchronous methods and tests #59

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,37 @@ function Promise(fn) {
})
}

this.state = state;

this.isFulfilled = function () {
debugger;
return state === true
}

this.isRejected = function () {
return state === false
}

this.isPending = function () {
return state === null
}

this.value = function () {
if (!this.isFulfilled()) {
throw new Error('Cannot get a value of an unfulfilled promise.')
}

return value
}

this.reason = function () {
if (!this.isRejected()) {
throw new Error('Cannot get a rejection reason of a non-rejected promise.')
}

return reason
}

function handle(deferred) {
if (state === null) {
deferreds.push(deferred)
Expand Down
154 changes: 154 additions & 0 deletions test/synchronous-inspection-tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
var assert = require('better-assert');
var Promise = require('../');

describe('synchronous-inspection-tests', function () {
it('can poll a promise to see if it is resolved', function () {
var finished = null;
var fulfilledPromise = new Promise(function(resolve, reject) {
var interval = setInterval(function() {
if (finished !== null) {
clearTimeout(interval);

if (finished) {
resolve(true);
}
else {
reject(false);
}
}
}, 10);
});

assert(fulfilledPromise.isPending());
assert(!fulfilledPromise.isFulfilled());
assert(!fulfilledPromise.isRejected());

finished = true;

setTimeout(function () {
assert(fulfilledPromise.isFulfilled());
assert(!fulfilledPromise.isRejected());
assert(fulfilledPromise.value());
assert(!fulfilledPromise.isPending());
}, 30);
});

it('can poll a promise to see if it is rejected', function () {
var finished = null;
var fulfilledPromise = new Promise(function(resolve, reject) {
var interval = setInterval(function() {
if (finished !== null) {
clearTimeout(interval);

if (finished) {
resolve(true);
}
else {
reject(false);
}
}
}, 10);
});

assert(fulfilledPromise.isPending());
assert(!fulfilledPromise.isFulfilled());
assert(!fulfilledPromise.isRejected());

finished = false;

setTimeout(function () {
assert(!fulfilledPromise.isFulfilled());
assert(fulfilledPromise.isRejected());
assert(!fulfilledPromise.reason());
assert(!fulfilledPromise.isPending());
}, 30);
});

it('will throw an error if getting a value of an unfulfilled promise', function () {
var finished = null;
var fulfilledPromise = new Promise(function(resolve, reject) {
var interval = setInterval(function() {
if (finished !== null) {
clearTimeout(interval);

if (finished) {
resolve(true);
}
else {
reject(false);
}
}
}, 10);
});

assert(fulfilledPromise.isPending());
assert(!fulfilledPromise.isFulfilled());
assert(!fulfilledPromise.isRejected());

try {
fulfilledPromise.value();

assert(false);
}
catch (e) {
assert(true);
}

finished = false;

setTimeout(function () {
try {
fulfilledPromise.value();

assert(false);
}
catch (e) {
assert(true);
}
}, 30);
});

it('will throw an error if getting a reason of a non-rejected promise', function () {
var finished = null;
var fulfilledPromise = new Promise(function(resolve, reject) {
var interval = setInterval(function() {
if (finished !== null) {
clearTimeout(interval);

if (finished) {
resolve(true);
}
else {
reject(false);
}
}
}, 10);
});

assert(fulfilledPromise.isPending());
assert(!fulfilledPromise.isFulfilled());
assert(!fulfilledPromise.isRejected());

try {
fulfilledPromise.reason();

assert(false);
}
catch (e) {
assert(true);
}

finished = true;

setTimeout(function () {
try {
fulfilledPromise.reason();

assert(false);
}
catch (e) {
assert(true);
}
}, 30);
});
});