UNPKG

control-flow

Version:

Turns asynchronous function into synchronous

84 lines (76 loc) 1.98 kB
var flow = require('../flow') var expect = require('expect.js') describe('Control Flow', function(){ var fn = function(arg, callback){ expect(arg).to.be('something') process.nextTick(function(){ callback(null, 'ok') }) } it('should provide promise', function(done){ flow.fiber(function(){ var result = yield(fn('something', flow.promise())) expect(result).to.be('ok') }, done) }), it('should synchronize function', function(done){ fn = flow.sync(fn) flow.fiber(function(){ expect(yield(fn('something'))).to.be('ok') }, done) }), it('should be save aginst synchronizing function twice', function(done){ fn = flow.sync(flow.sync(fn)) flow.fiber(function(){ expect(yield(fn('something'))).to.be('ok') }, done) }), it('should allow call synchronized function explicitly', function(done){ fn = flow.sync(fn) fn('something', function(err, result){ expect(result).to.be('ok') done(err) }) }), it("should catch asynchronous errors", function(done){ var fn = function(callback){ process.nextTick(function(){ callback(new Error('an error')) }) } fn = flow.sync(fn) flow.fiber(function(){ var err try { yield(fn()) } catch (e) { err = e } expect(err.message).to.be('an error') }, done) }), it("should be compatible with not asynchronous callbacks", function(done){ fn = function(callback){ callback(null, 'ok') } fn = flow.sync(fn) flow.fiber(function(){ expect(yield(fn())).to.be('ok') }, done) }), it("should catch non asynchronous errors", function(done){ fn = function(callback){ callback(new Error('an error')) } fn = flow.sync(fn) flow.fiber(function(){ var err try { yield(fn()) } catch (e) { err = e } expect(err.message).to.be('an error') }, done) }) })