Permalink
Browse files

Made async execution optional

  • Loading branch information...
1 parent 442a6c8 commit 9661766abbae5848b9ebc2489042862f24d62350 @Leonidas-from-XIV committed Feb 28, 2013
Showing with 32 additions and 8 deletions.
  1. +4 −0 README.md
  2. +16 −6 lib/xml2js.js
  3. +12 −2 src/xml2js.coffee
View
@@ -187,6 +187,10 @@ value})``. Possible options are:
`explicitChildren` is set to `true`. Added in 0.2.5.
* `charsAsChildren` (default `false`): Determines whether chars should be
considered children if `explicitChildren` is on. Added in 0.2.5.
+ * `async` (default `false`): Should the callbacks be async? This *might* be
+ an incompatible change if your code depends on sync execution of callbacks.
+ xml2js 0.3 might change this default, so the recommendation is to not
+ depend on sync execution anyway. Added in 0.2.6.
Updating to new version
=======================
View
@@ -29,7 +29,8 @@
xmlns: false,
explicitChildren: false,
childkey: '@@',
- charsAsChildren: false
+ charsAsChildren: false,
+ async: false
},
"0.2": {
explicitCharkey: false,
@@ -46,7 +47,8 @@
xmlns: false,
explicitChildren: false,
childkey: '$$',
- charsAsChildren: false
+ charsAsChildren: false,
+ async: false
}
};
@@ -229,15 +231,23 @@
if ((cb != null) && typeof cb === "function") {
this.on("end", function(result) {
this.reset();
- return process.nextTick(function() {
+ if (this.options.async) {
+ return process.nextTick(function() {
+ return cb(null, result);
+ });
+ } else {
return cb(null, result);
- });
+ }
});
this.on("error", function(err) {
this.reset();
- return process.nextTick(function() {
+ if (this.options.async) {
+ return process.nextTick(function() {
+ return cb(err);
+ });
+ } else {
return cb(err);
- });
+ }
});
}
if (str.toString().trim() === '') {
View
@@ -31,6 +31,8 @@ exports.defaults =
explicitChildren: false
childkey: '@@'
charsAsChildren: false
+ # callbacks are async? not in 0.1 mode
+ async: false
"0.2":
explicitCharkey: false
@@ -48,6 +50,8 @@ exports.defaults =
explicitChildren: false
childkey: '$$'
charsAsChildren: false
+ # not async in 0.2 mode either
+ async: false
class exports.ValidationError extends Error
constructor: (message) ->
@@ -193,11 +197,17 @@ class exports.Parser extends events.EventEmitter
if cb? and typeof cb is "function"
@on "end", (result) ->
@reset()
- process.nextTick ->
+ if @options.async
+ process.nextTick ->
+ cb null, result
+ else
cb null, result
@on "error", (err) ->
@reset()
- process.nextTick ->
+ if @options.async
+ process.nextTick ->
+ cb err
+ else
cb err
if str.toString().trim() is ''

2 comments on commit 9661766

@JacksonTian
Contributor

When execute sync, should tell user try/catch exception that threw by callback.

@JacksonTian
Contributor

The saxjs can parse xml with pipe/stream, it means the callback maybe or not be executed blocking. So the callback should be executed async for more robust.

But xml2js parses the xml only once, it approximately equal sync. Maybe it's the reason why some guys think it be executed sync. - -

In node, the default should be async. If need blocking version, add Sync please.

Please sign in to comment.