Last active
January 1, 2016 00:09
-
-
Save hipertracker/8064847 to your computer and use it in GitHub Desktop.
Meteor with (1) Npm and Async tools, (2) q.js promises
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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