Skip to content

Instantly share code, notes, and snippets.

@hipertracker
Last active January 1, 2016 00:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hipertracker/8064847 to your computer and use it in GitHub Desktop.
Save hipertracker/8064847 to your computer and use it in GitHub Desktop.
Meteor with (1) Npm and Async tools, (2) q.js promises
// feature: blocking and server side only
// uses: https://github.com/arunoda/meteor-npm
// install: mrt add npm
blockingRequest = function (verb, url, data) {
return Async.runSync(function (done) {
HTTP.call(verb, url, data, function (error, result) {
done(error, result);
});
});
};
// example:
console.log('START');
var response = blockingRequest('GET', 'http://localhost/api/books');
console.log('ERROR:', response.error);
console.log('DATA:', response.result);
console.log('END');
// feature: non blocking, server and client side
// uses: https://github.com/kriskowal/q
// install: mrt add q
nonBlockingRequest = function (verb, url, data) {
var deferred = Q.defer();
HTTP.call(verb, url, data, function (error, result) {
deferred.reject(error);
deferred.resolve(result);
});
return deferred.promise;
};
// example:
console.log('START');
nonBlockingRequest('GET', 'http://localhost/api/books').then(function (result) {
console.log('DATA:', result);
}, function (error) {
console.log('ERROR:', error)
});
console.log('END');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment