Skip to content

Commit

Permalink
Async Callbacks (wit-ai#9)
Browse files Browse the repository at this point in the history
* Async Callbacks

* Small fix
  • Loading branch information
DiegoRBaquero authored and patapizza committed Apr 14, 2016
1 parent a753676 commit 30f3888
Showing 1 changed file with 33 additions and 11 deletions.
44 changes: 33 additions & 11 deletions lib/wit.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,17 @@ const makeWitResponseHandler = (endpoint, l, cb) => (
if (err) {
l.error('[' + endpoint + '] Error: ' + err);
if (cb) {
cb(err);
process.nextTick(() => {
cb(err);
});
}
return;
}
l.debug('[' + endpoint + '] Response: ' + JSON.stringify(data));
if (cb) {
cb(null, data);
process.nextTick(() => {
cb(null, data);
});
}
}
);
Expand Down Expand Up @@ -115,7 +119,9 @@ const Wit = function(token, actions, logger) {
error = error || !json.type && 'Couldn\'t find type in Wit response';
if (error) {
if (cb) {
cb(error, context);
process.nextTick(() => {
cb(error, context);
});
}
return;
}
Expand All @@ -124,13 +130,17 @@ const Wit = function(token, actions, logger) {
if (json.type === 'stop') {
// End of turn
if (cb) {
cb(null, context);
process.nextTick(() => {
cb(null, context);
});
}
return;
} else if (json.type === 'msg') {
if (!this.actions.say) {
if (cb) {
cb('No \'say\' action found.');
process.nextTick(() => {
cb('No \'say\' action found.');
});
}
return;
}
Expand All @@ -144,7 +154,9 @@ const Wit = function(token, actions, logger) {
if (i <= 0) {
l.warn('Max steps reached, halting.');
if (cb) {
cb(null, context);
process.nextTick(() => {
cb(null, context);
});
}
return;
}
Expand All @@ -160,7 +172,9 @@ const Wit = function(token, actions, logger) {
} else if (json.type === 'merge') {
if (!this.actions.merge) {
if (cb) {
cb('No \'merge\' action found.');
process.nextTick(() => {
cb('No \'merge\' action found.');
});
}
return;
}
Expand All @@ -177,7 +191,9 @@ const Wit = function(token, actions, logger) {
if (i <= 0) {
l.warn('Max steps reached, halting.');
if (cb) {
cb(null, context);
process.nextTick(() => {
cb(null, context);
});
}
return;
}
Expand All @@ -194,7 +210,9 @@ const Wit = function(token, actions, logger) {
const action = json.action;
if (!this.actions.hasOwnProperty(action)) {
if (cb) {
cb('No \'' + action + '\' action found.', context);
process.nextTick(() => {
cb('No \'' + action + '\' action found.', context);
});
}
return;
}
Expand All @@ -213,7 +231,9 @@ const Wit = function(token, actions, logger) {
if (i <= 0) {
l.warn('Max steps reached, halting.');
if (cb) {
cb(null, context);
process.nextTick(() => {
cb(null, context);
});
}
return;
}
Expand All @@ -229,7 +249,9 @@ const Wit = function(token, actions, logger) {
} else { // error
if (!this.actions.error) {
if (cb) {
cb('No \'error\' action found.');
process.nextTick(() => {
cb('No \'error\' action found.');
});
}
return;
}
Expand Down

0 comments on commit 30f3888

Please sign in to comment.