Skip to content

Commit

Permalink
Fix the ordering of callbacks using process.nextTick
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewtole committed Apr 24, 2015
1 parent 55a9505 commit d3417f3
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions lib/timeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ Timeline.Pin = Pin;
*/
Timeline.prototype.sendUserPin = function (userToken, pin, callback) {
if (typeof userToken !== 'string') {
return callback(new Error('Expected userToken to be a string.'));
process.nextTick(callback(new Error('Expected userToken to be a string.')));
return;
}

if (!(pin instanceof Pin)) {
Expand Down Expand Up @@ -59,11 +60,13 @@ Timeline.prototype.sendUserPin = function (userToken, pin, callback) {
*/
Timeline.prototype.sendSharedPin = function (topics, pin, callback) {
if (!this._apiKey) {
return callback(new Error('API Key not set.'));
process.nextTick(callback(new Error('API Key not set.')));
return;
}

if (!(topics instanceof Array)) {
return callback(new Error('Expected topics to be an array.'));
process.nextTick(callback(new Error('Expected topics to be an array.')))
return;
}

if (!(pin instanceof Pin)) {
Expand Down Expand Up @@ -92,7 +95,8 @@ Timeline.prototype.sendSharedPin = function (topics, pin, callback) {
*/
Timeline.prototype.deleteUserPin = function (userToken, pin, callback) {
if (typeof userToken !== 'string') {
return callback(new Error('Expected userToken to be a string.'));
process.nextTick(callback(new Error('Expected userToken to be a string.')));
return;
}

if (!(pin instanceof Pin)) {
Expand All @@ -119,11 +123,13 @@ Timeline.prototype.deleteUserPin = function (userToken, pin, callback) {
*/
Timeline.prototype.subscribe = function (userToken, topic, callback) {
if (typeof userToken !== 'string') {
return callback(new Error('Expected userToken to be a string.'));
process.nextTick(callback(new Error('Expected userToken to be a string.')));
return;
}

if (typeof topic !== 'string') {
return callback(new Error('Expected topic to be a string.'));
process.nextTick(callback(new Error('Expected topic to be a string.')));
return;
}

var opts = {
Expand All @@ -146,11 +152,13 @@ Timeline.prototype.subscribe = function (userToken, topic, callback) {
*/
Timeline.prototype.unsubscribe = function (userToken, topic, callback) {
if (typeof userToken !== 'string') {
return callback(new Error('Expected userToken to be a string.'));
process.nextTick(callback(new Error('Expected userToken to be a string.')));
return;
}

if (typeof topic !== 'string') {
return callback(new Error('Expected topic to be a string.'));
process.nextTick(callback(new Error('Expected topic to be a string.')));
return;
}

var opts = {
Expand Down Expand Up @@ -184,15 +192,18 @@ Timeline.prototype._request = function(opts, callback) {

request(reqOpts, function (err, res, body) {
if (err) {
return callback(err, body, res);
callback(err, body, res);
return;
}

if (res.statusCode < 300) {
return callback(null, body, res);
callback(null, body, res);
return;
}

if (Timeline.errorCodes[res.statusCode]) {
return callback(new Error(Timeline.errorCodes[res.statusCode]), body, res);
callback(new Error(Timeline.errorCodes[res.statusCode]), body, res);
return;
}

callback(new Error('Unknown error. Status code: ' + res.statusCode), body, res);
Expand Down

0 comments on commit d3417f3

Please sign in to comment.