Skip to content

Commit

Permalink
Actually 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 d3417f3 commit b804a32
Showing 1 changed file with 27 additions and 22 deletions.
49 changes: 27 additions & 22 deletions lib/timeline.js
Expand Up @@ -31,8 +31,9 @@ Timeline.Pin = Pin;
*/
Timeline.prototype.sendUserPin = function (userToken, pin, callback) {
if (typeof userToken !== 'string') {
process.nextTick(callback(new Error('Expected userToken to be a string.')));
return;
return process.nextTick(function () {
callback(new Error('Expected userToken to be a string.'));
});
}

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

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

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

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

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

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

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

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

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

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

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

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

0 comments on commit b804a32

Please sign in to comment.