Skip to content

Commit

Permalink
final resolution is to subordinate nextTick to setImmediate
Browse files Browse the repository at this point in the history
  • Loading branch information
wizardwerdna committed Apr 5, 2013
1 parent f1925b5 commit fdaf44b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 28 deletions.
10 changes: 5 additions & 5 deletions covenant.coffee
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
root = (exports ? this)

root.bestImmediate = bestImmediate =
root.enqueue = enqueue =
(typeof setImmediate == 'function' && setImmediate) or
(process?.nextTick) or
(task) -> setTimeout(task, 0)
root.bestTick = bestTick = (process?.nextTick) or root.bestImmediate

class Covenant
constructor: -> @state = new PendingState
Expand Down Expand Up @@ -39,7 +39,7 @@ class CompletedState
p2.reject e
_handleFunctionResult: (datum, callback, fallback, p2) ->
if @_isPromise result=callback(datum)
bestImmediate => result.then p2.fulfill, p2.reject
result.then p2.fulfill, p2.reject
else
p2.fulfill result
_isFunction: (thing)-> typeof thing is 'function'
Expand All @@ -48,9 +48,9 @@ class CompletedState
class FulfilledState extends CompletedState
constructor: (@value, pended) -> super pended
_schedule: (onFulfill, __, p2) ->
bestTick => @_do @value, onFulfill, p2.fulfill, p2
enqueue => @_do @value, onFulfill, p2.fulfill, p2

class RejectedState extends CompletedState
constructor: (@reason, pended) -> super pended
_schedule: (__, onReject, p2) ->
bestTick => @_do @reason, onReject, p2.reject, p2
enqueue => @_do @reason, onReject, p2.reject, p2
20 changes: 10 additions & 10 deletions test/covenant.coffee
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
should = require 'should'
{Covenant, bestTick} = require('../covenant')
{Covenant} = require('../covenant')

# test scaffolding
p = p2 = p3 = returnPromise = callback = null
Expand Down Expand Up @@ -53,7 +53,7 @@ describe "Covenant", ->
beforeEach -> p.fulfill(dummy)
it ", p2=p.then(nonFunction, __) returns p2 fulfilled with value", (done)->
p2 = p.then(undefined, undefined)
bestTick ->
setImmediate ->
p2.state.value.should.eql dummy
done()
describe ", p2=p.then(function, __)", ->
Expand All @@ -64,13 +64,13 @@ describe "Covenant", ->
describe ", and function returns a non-promise value", ->
it "p2 is fulfilled with the returned value", (done)->
p2 = p.then( (->dummy2), undefined)
bestTick ->
setImmediate ->
p2.state.value.should.eql dummy2
done()
describe ", and function throws an exception", ->
it "p2 is rejected with the exception as its reason", (done)->
p2 = p.then( (->throw dummyReason), undefined )
bestTick ->
setImmediate ->
p2.state.reason.should.eql dummyReason
done()
describe ", and function returns a promise", ->
Expand All @@ -96,7 +96,7 @@ describe "Covenant", ->
beforeEach -> p.reject(dummy)
it ", p2=p.then(__, nonFunction) returns p2 rejected with reason", (done)->
p2 = p.then(undefined, undefined)
bestTick ->
setImmediate ->
p2.state.reason.should.eql dummy
done()
describe ", p2=p.then(__, function)", ->
Expand All @@ -105,13 +105,13 @@ describe "Covenant", ->
describe ", and function returns a non-promise value", ->
it "p2 is fulfilled with the returned value", (done)->
p2 = p.then( undefined, (->dummy2))
bestTick ->
setImmediate ->
p2.state.value.should.eql dummy2
done()
describe ", and function throws an exception", ->
it "p2 is rejected with the exception as its reason", (done)->
p2 = p.then( undefined, (->throw dummyReason))
bestTick ->
setImmediate ->
p2.state.reason.should.eql dummyReason
done()
describe ", and function returns a promise", ->
Expand Down Expand Up @@ -141,19 +141,19 @@ describe "Covenant", ->
type.should.eql 'function'
it ", after p.fulfill(value), p2 is fulfilled with value", (done)->
p.fulfill(dummy)
bestTick ->
setImmediate ->
p2.state.value.should.eql dummy
done()
it ", after reject(reason), p2 is rejected with value", (done)->
p.reject(dummyReason)
bestTick ->
setImmediate ->
p2.state.reason.should.eql dummyReason
done()
describe ", and then p3=p.then(onFulfil, onReject)", ->
it "p2 should be fulfilled after p.fulfill", (done)->
p3=p.then( )
p.fulfill(dummy)
bestTick ->
setImmediate ->
testFulfilled(p2)
done()
it "p2 and p3 should be fulfilled in sequence", (done) ->
Expand Down
26 changes: 13 additions & 13 deletions test/promise.coffee
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
should = require 'should'
{Promise, bestTick} = require('../promise')
{Promise} = require('../promise')

# test scaffold
p = p1 = p2 = p3 = returnPromise = callback = null
Expand Down Expand Up @@ -54,7 +54,7 @@ describe "Promise", ->
beforeEach -> p.fulfill(dummy)
it ", p2=p.done(nonFunction) returns p2 fulfilled with value", (done)->
p2 = p.done(undefined)
bestTick ->
setImmediate ->
p2.state.value.should.eql dummy
done()
describe ", p2=p.done(function)", ->
Expand All @@ -63,13 +63,13 @@ describe "Promise", ->
describe ", and function returns a non-promise value", ->
it "p2 is fulfilled with the returned value", (done)->
p2 = p.done ->dummy2
bestTick ->
setImmediate ->
p2.state.value.should.eql dummy2
done()
describe ", and function throws an exception", ->
it "p2 is rejected with the exception as its reason", (done)->
p2 = p.done ->throw dummyReason
bestTick ->
setImmediate ->
p2.state.reason.should.eql dummyReason
done()
describe ", and function returns a promise", ->
Expand All @@ -95,7 +95,7 @@ describe "Promise", ->
beforeEach -> p.reject(dummy)
it ", p2=p.fail(nonFunction) returns p2 rejected with reason", (done)->
p2 = p.fail(undefined)
bestTick ->
setImmediate ->
p2.state.reason.should.eql dummy
done()
describe ", p2=p.fail(function)", ->
Expand All @@ -104,13 +104,13 @@ describe "Promise", ->
describe ", and function returns a non-promise value", ->
it "p2 is fulfilled with the returned value", (done)->
p2 = p.fail -> dummy2
bestTick ->
setImmediate ->
p2.state.value.should.eql dummy2
done()
describe ", and function throws an exception", ->
it "p2 is rejected with the exception as its reason", (done)->
p2 = p.fail ->throw dummyReason
bestTick ->
setImmediate ->
p2.state.reason.should.eql dummyReason
done()
describe ", and function returns a promise", ->
Expand All @@ -136,7 +136,7 @@ describe "Promise", ->
beforeEach -> p.fulfill(dummy)
it ", p2=p.always(nonFunction) returns p2 fulfilled with value", (always)->
p2 = p.always(undefined)
bestTick ->
setImmediate ->
p2.state.value.should.eql dummy
always()
describe ", p2=p.always(function)", ->
Expand All @@ -145,13 +145,13 @@ describe "Promise", ->
describe ", and function returns a non-promise value", ->
it "p2 is fulfilled with the returned value", (always)->
p2 = p.always ->dummy2
bestTick ->
setImmediate ->
p2.state.value.should.eql dummy2
always()
describe ", and function throws an exception", ->
it "p2 is rejected with the exception as its reason", (always)->
p2 = p.always ->throw dummyReason
bestTick ->
setImmediate ->
p2.state.reason.should.eql dummyReason
always()
describe ", and function returns a promise", ->
Expand All @@ -177,7 +177,7 @@ describe "Promise", ->
beforeEach -> p.reject(dummy)
it ", p2=p.always(nonFunction) returns p2 rejected with reason", (always)->
p2 = p.always(undefined)
bestTick ->
setImmediate ->
p2.state.reason.should.eql dummy
always()
describe ", p2=p.always(function)", ->
Expand All @@ -186,13 +186,13 @@ describe "Promise", ->
describe ", and function returns a non-promise value", ->
it "p2 is fulfilled with the returned value", (always)->
p2 = p.always -> dummy2
bestTick ->
setImmediate ->
p2.state.value.should.eql dummy2
always()
describe ", and function throws an exception", ->
it "p2 is rejected with the exception as its reason", (always)->
p2 = p.always ->throw dummyReason
bestTick ->
setImmediate ->
p2.state.reason.should.eql dummyReason
always()
describe ", and function returns a promise", ->
Expand Down

0 comments on commit fdaf44b

Please sign in to comment.