Commit 543306a7 authored by Romain Courteaud's avatar Romain Courteaud

Add RSVP#delay, promise#delay, RSVP#timeout

parent c2973910
......@@ -3,6 +3,7 @@ import { CancellationError } from "./rsvp/cancellation_error";
import { Promise } from "./rsvp/promise";
import { denodeify } from "./rsvp/node";
import { all } from "./rsvp/all";
import { delay, timeout } from "./rsvp/timeout";
import { hash } from "./rsvp/hash";
import { rethrow } from "./rsvp/rethrow";
import { defer } from "./rsvp/defer";
......@@ -14,4 +15,4 @@ function configure(name, value) {
config[name] = value;
}
export { CancellationError, Promise, EventTarget, all, hash, rethrow, defer, denodeify, configure, resolve, reject };
export { CancellationError, Promise, EventTarget, all, delay, timeout, hash, rethrow, defer, denodeify, configure, resolve, reject };
import { Promise } from "./promise";
function promiseSetTimeout(millisecond, should_reject, message) {
var timeout_id;
function resolver(resolve, reject) {
timeout_id = setTimeout(function () {
if (should_reject) {
reject(message);
} else {
resolve(message);
}
}, millisecond);
}
function canceller() {
clearTimeout(timeout_id);
}
return new Promise(resolver, canceller);
}
function delay(millisecond, message) {
return promiseSetTimeout(millisecond, false, message);
}
function timeout(millisecond) {
return promiseSetTimeout(millisecond, true,
"Timed out after " + millisecond + " ms");
}
Promise.prototype.delay = function(millisecond) {
return this.then(function (fulfillmentValue) {
return delay(millisecond, fulfillmentValue);
});
};
export { delay, timeout };
......@@ -1389,3 +1389,114 @@ describe("`always` on a promise", function () {
}, 20);
});
});
describe("`RSVP.delay`", function () {
it('should be a function', function () {
assert.equal(typeof RSVP.delay, "function");
});
it('should return a new promise', function () {
var promise = RSVP.delay();
assert(promise instanceof RSVP.Promise);
});
it('should fulfill the new promise after some milliseconds', function (done) {
var promise = RSVP.delay(30);
setTimeout(function() {
assert.equal(promise.isFulfilled, undefined);
assert.equal(promise.isRejected, undefined);
setTimeout(function() {
assert.equal(promise.isFulfilled, true);
assert.equal(promise.isRejected, undefined);
done();
}, 20);
}, 20);
});
it('should allow to cancel the setTimeout', function (done) {
var promise = RSVP.delay();
promise.cancel();
setTimeout(function() {
// XXX How to check that clearTimeout is called?
assert.equal(promise.isRejected, true);
done();
}, 20);
});
});
describe("`promise.delay`", function () {
it('should be a function', function () {
var promise = new RSVP.Promise(function (accept, fail) {});
assert.equal(typeof promise.delay, "function");
});
it('should return a new promise', function () {
var promise = new RSVP.Promise(function (accept, fail) {}),
promise2 = promise.delay();
assert(promise2 instanceof RSVP.Promise);
});
it('should fulfill the new promise after some milliseconds', function (done) {
var promise = new RSVP.Promise(function (accept, fail) {accept();}),
promise2 = promise.delay(30);
setTimeout(function() {
assert.equal(promise2.isFulfilled, undefined);
assert.equal(promise2.isRejected, undefined);
setTimeout(function() {
assert.equal(promise2.isFulfilled, true);
assert.equal(promise2.isRejected, undefined);
done();
}, 20);
}, 20);
});
it('should propagate the fulfillmentValue', function (done) {
var promise = new RSVP.Promise(function (accept, fail) {accept("foo");}),
promise2 = promise.delay(30);
setTimeout(function() {
assert.equal(promise2.isFulfilled, true);
assert.equal(promise2.isRejected, undefined);
assert.equal(promise2.fulfillmentValue, "foo");
done();
}, 40);
});
});
describe("`RSVP.timeout`", function () {
it('should be a function', function () {
assert.equal(typeof RSVP.timeout, "function");
});
it('should return a new promise', function () {
var promise = RSVP.timeout();
assert(promise instanceof RSVP.Promise);
});
it('should reject the new promise after some milliseconds', function (done) {
var promise = RSVP.timeout(30);
setTimeout(function() {
assert.equal(promise.isFulfilled, undefined);
assert.equal(promise.isRejected, undefined);
setTimeout(function() {
assert.equal(promise.isFulfilled, undefined);
assert.equal(promise.isRejected, true);
assert.equal(promise.rejectedReason, "Timed out after 30 ms");
done();
}, 20);
}, 20);
});
it('should allow to cancel the setTimeout', function (done) {
var promise = RSVP.timeout();
promise.cancel();
setTimeout(function() {
// XXX How to check that clearTimeout is called?
assert.equal(promise.isRejected, true);
done();
}, 20);
});
});
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