synchronize
Advanced tools
Comparing version 0.9.12 to 0.9.13
{ | ||
"name" : "synchronize", | ||
"main" : "./sync", | ||
"version" : "0.9.12", | ||
"version" : "0.9.13", | ||
"homepage" : "http://alexeypetrushin.github.com/synchronize", | ||
@@ -6,0 +6,0 @@ "repository": { |
@@ -17,2 +17,6 @@ synchronize.js allows You write asynchronous code as if it's synchronous. | ||
Other resources: | ||
-[Patterns and anti-patterns using synchronize.js](https://mixmax.com/blog/node-fibers-using-synchronize-js) | ||
Copyright (c) Alexey Petrushin, http://petrush.in, released under the MIT license. |
10
sync.js
@@ -246,2 +246,12 @@ /*jshint node: true, indent:2, loopfunc: true, asi: true, undef:true*/ | ||
}finally{ | ||
// If neither `defer` nor `defers` were called within the callback, we need | ||
// to run the fiber ourselves or else the fiber will unwind when `await` is called. | ||
// This might happen if the user intended to enumerate an array within the | ||
// callback, calling `defer` once per array item, but the array was empty. | ||
if (!fiber._syncParallel.called) { | ||
process.nextTick(function(){ | ||
// Return an empty array to represent that there were no results. | ||
if(!fiber._syncIsTerminated) fiber.run([]) | ||
}) | ||
} | ||
delete fiber._syncParallel | ||
@@ -248,0 +258,0 @@ } |
@@ -0,1 +1,3 @@ | ||
/*jshint node: true, indent:2, loopfunc: true, asi: true, undef:true, mocha: true */ | ||
// require('longjohn') | ||
@@ -251,2 +253,12 @@ | ||
it('should not unwind when await is called after an empty parallel block', function(done){ | ||
sync.fiber(function(){ | ||
sync.parallel(function(){ | ||
// Imagine that the user intends to enumerate an array here, calling | ||
// `defer` once per array item, but the array is empty. | ||
}) | ||
expect(sync.await()).to.eql([]) | ||
}, done) | ||
}) | ||
it('should return result from fiber', function(done){ | ||
@@ -253,0 +265,0 @@ sync.fiber(function(){ |
202317
6184
22