fjakobs / async.js Goto Github PK
View Code? Open in Web Editor NEWasync made easy
async made easy
When I used "until" with a 1000 or so iterations, node is saying "Maximum call stack size exceeded". Is there a limit to the number of iterations? My code was working and I think I've coded it properly. FWIW, here is my code.
async.until(function() {
return opt.eof && !buf;
}, function(cb) {
var delim, match, parts;
if ((parts = regex.exec(buf))) {
match = parts[0], delim = parts[1], buf = parts[2];
switch (opt.delimType) {
case 'prefix':
opt.data = prefix + match;
prefix = delim;
break;
case 'suffix':
opt.data = match + delim;
break;
default:
opt.data = match;
}
callback(opt);
return stopChk(cb);
} else if (opt.eof) {
opt.data = prefix + buf;
callback(opt);
opt.stop = true;
return stopChk(cb);
} else {
return fs.read(fd, inBuf, 0, opt.bufSize, null, function(err, bytesRead) {
if (err) {
return cb(err);
} else {
if (bytesRead) {
buf += inBuf.toString('utf8', 0, bytesRead);
return cb();
} else {
opt.eof = true;
return fs.close(fd, cb);
}
}
});
}
});
return function(err) {
if (!opt.stop) {
opt.err = err;
return callback(opt);
}
};
)
Coffeescript original source ...
async.until \
-> (opt.eof and not buf),
(cb) ->
if (parts = regex.exec buf)
[match, delim, buf] = parts
switch opt.delimType
when 'prefix' then opt.data = prefix + match; prefix = delim
when 'suffix' then opt.data = match + delim
else opt.data = match
callback opt
stopChk cb
else if opt.eof
opt.data = prefix + buf
callback opt
opt.stop = true
stopChk cb
else
fs.read fd, inBuf, 0, opt.bufSize, null, (err, bytesRead) ->
if err then cb err
else
if bytesRead
buf += inBuf.toString 'utf8', 0, bytesRead
cb()
else
opt.eof = true; fs.close fd, cb
(err) ->
if not opt.stop
opt.err = err
callback opt
glob finds files that should not match my pattern. Interesting enough, this happens only on the first level.
xy:/tmp/foo$ ls -R .
.:
test test.js
./test:
dir1 file1.foo file2.foo file5.bar
./test/dir1:
file3.foo file4.bar
xy:/tmp/foo$ cat test.js
var sys = require('sys'),
async = require('asyncjs');
async.glob(__dirname + '/test/*/*.foo').toArray(function(err, files) {
sys.puts(sys.inspect(files));
});
xy:/tmp/foo$ node test.js
[ { path: '/tmp/foo/test/file1.foo', name: 'file1.foo' }
, { path: '/tmp/foo/test/file5.bar', name: 'file5.bar' }
, { path: '/tmp/foo/test/file2.foo', name: 'file2.foo' }
, { path: '/tmp/foo/test/dir1/file3.foo'
, name: 'file3.foo'
, stat: { ... }
}
]
Please release a new release since last release was 0.1 in 2010.
Consequent feature like in toArray
There are no tests to check if the exists
functions works.
On repeated runs in the test directory of
node all.js
some pass all tests and others have seemingly random errors come up.
For example,
[22/17] fs: test glob with only file magic FAIL
TypeError: Bad argument
at Object.read (fs:177:11)
at [object Object]._read (fs:728:6)
at [object Object].resume (fs:796:8)
at [object Object]. (sys:298:16)
at [object Object].emit (events:26:26)
at [object Object].flush (fs:843:26)
at fs:882:10
at node.js:775:9
and on another run
[22/13] fs: test walk files post order FAIL
TypeError: Bad argument
at Object.read (fs:177:11)
at [object Object]._read (fs:728:6)
at [object Object].resume (fs:796:8)
at [object Object]. (sys:298:16)
at [object Object].emit (events:26:26)
at [object Object].flush (fs:843:26)
at fs:882:10
at node.js:775:9
The current drivers are running serially - every item in the list waits for the previous one to be ready. If a filter does not depend on previous results, its generator can be executed in parallel, so that in the best case, only max(time) of all called filters is needed for execution. It's even more interesting for bigger applications that use distributed map operations.
A test case that I would like to transform into a patch: https://gist.github.com/734436
Today I will make some more tests based on a real world case with DB queries.
This is generated code from CoffeeScript that runs in node 0.4.5 and with latest asyncjs. I use the "=>" operator, that prefixes a function's body and preserves the current context (this):
var async;
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
async = require('asyncjs');
async.list([1, 2, 3]).forEach(__bind(function(name, next) {
return next(null, name);
}, this)).toArray(false, function(e, f) {
return console.log(f);
});
Result:
[ 1, 2, 3 ]
[ 1, 2, 3, 3 ]
[ 1, 2, 3, 3, 2 ]
[ 1, 2, 3, 3, 2, 1 ]
Instead of:
[ 1, 2, 3 ]
Is the function eachInSeries
needed? This would call each item in the generator in a series instead of in parallel as the current each
. If so, I could try to write a plugin but wanted to check first.
This is a great lib, thanks.
$ node all.js
set up suite
[6/1] async: test toArray OK
...
[22/12] fs: test walk files pre order FAIL
AssertionError: "[\"assets_TEST/walk\",\"assets_TEST/walk/dir1\",\"assets_TEST/walk/dir1/1.txt\",\"assets_TEST/walk/dir2\",\"assets_TEST/walk/dir2/dir22\",\"assets_TEST/walk/dir2/dir22/22.txt\",\"assets_TEST/walk/dir2/2.txt\",\"assets_TEST/walk/1.txt\"]" == "[\"assets_TEST/walk\",\"assets_TEST/walk/dir2\",\"assets_TEST/walk/dir2/dir22\",\"assets_TEST/walk/dir2/dir22/22.txt\",\"assets_TEST/walk/dir2/2.txt\",\"assets_TEST/walk/dir1\",\"assets_TEST/walk/dir1/1.txt\",\"assets_TEST/walk/1.txt\"]"
at /home/andi/.node_libraries/async.js/test/fs.test.js:192:24
at handler (/home/andi/.node_libraries/async.js/lib/async/async.js:359:32)
at /home/andi/.node_libraries/async.js/lib/async/async.js:33:21
at handler (/home/andi/.node_libraries/async.js/lib/async/async.js:245:36)
at Object.next (/home/andi/.node_libraries/async.js/lib/async/async.js:406:13)
at [object Object].next (/home/andi/.node_libraries/async.js/lib/async/async.js:20:21)
at Object.next (/home/andi/.node_libraries/async.js/lib/async/async.js:240:20)
at [object Object].next (/home/andi/.node_libraries/async.js/lib/async/async.js:20:21)
at Object.next (/home/andi/.node_libraries/async.js/lib/async/plugins/fs-node.js:287:28)
at [object Object].next (/home/andi/.node_libraries/async.js/lib/async/async.js:31:20)
[22/13] fs: test walk files post order FAIL
AssertionError: "[\"assets_TEST/walk/dir1/1.txt\",\"assets_TEST/walk/dir1\",\"assets_TEST/walk/dir2/dir22/22.txt\",\"assets_TEST/walk/dir2/dir22\",\"assets_TEST/walk/dir2/2.txt\",\"assets_TEST/walk/dir2\",\"assets_TEST/walk/1.txt\",\"assets_TEST/walk\"]" == "[\"assets_TEST/walk/dir2/dir22/22.txt\",\"assets_TEST/walk/dir2/dir22\",\"assets_TEST/walk/dir2/2.txt\",\"assets_TEST/walk/dir2\",\"assets_TEST/walk/dir1/1.txt\",\"assets_TEST/walk/dir1\",\"assets_TEST/walk/1.txt\",\"assets_TEST/walk\"]"
at /home/andi/.node_libraries/async.js/test/fs.test.js:213:24
at handler (/home/andi/.node_libraries/async.js/lib/async/async.js:359:32)
at /home/andi/.node_libraries/async.js/lib/async/async.js:33:21
at handler (/home/andi/.node_libraries/async.js/lib/async/async.js:245:36)
at Object.next (/home/andi/.node_libraries/async.js/lib/async/async.js:406:13)
at [object Object].next (/home/andi/.node_libraries/async.js/lib/async/async.js:20:21)
at Object.next (/home/andi/.node_libraries/async.js/lib/async/async.js:240:20)
at [object Object].next (/home/andi/.node_libraries/async.js/lib/async/async.js:20:21)
at Object.next (/home/andi/.node_libraries/async.js/lib/async/plugins/fs-node.js:287:28)
at [object Object].next (/home/andi/.node_libraries/async.js/lib/async/async.js:31:20)
[22/14] fs: test glob without magic OK
...
[22/22] fs: test glob with * in path and ? name OK
Summary:
Total number of tests: 28
Passed tests: 26
Failed tests: 2
It seems that there is still no way to reset the generator to the first value. I need to recreate the whole thing to do this. I can send you a pull request.
Can we have a patch release published to npm please? Would like to get access to the fs.exists deprecation change merged quite a while ago.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.