Created
September 1, 2014 01:47
-
-
Save scudelletti/542f9ad96e6574bc6ba9 to your computer and use it in GitHub Desktop.
[NODE] Javascript Promises using Q library simple examples
This file contains hidden or 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
// Using callbacks to retrieve Weather Data | |
var RestClient = require('restler') | |
var getURL = function(city){ | |
return 'http://api.openweathermap.org/data/2.5/weather?q=' + city; | |
}; | |
var dateOne = new Date(); | |
RestClient.get(getURL('London')).on('complete', function(data1){ | |
RestClient.get(getURL('Sao Paulo')).on('complete', function(data2){ | |
console.log([data1.name, data2.name]); | |
var dateTwo = new Date(); | |
console.log(dateTwo.getTime() - dateOne.getTime()); | |
}); | |
}); |
This file contains hidden or 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
var Q = require('q'); | |
var sum = function() { | |
var d = Q.defer(); | |
setTimeout(function() { | |
d.resolve(1); | |
}, 3000); | |
return d.promise; | |
}; | |
var second = function(first) { | |
var d = Q.defer(); | |
setTimeout(function() { | |
d.resolve(first + 2); | |
}, 3000); | |
return d.promise; | |
}; | |
var end = function(result) { console.log(result) }; | |
sum().then(second).done(end) |
This file contains hidden or 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
var Q = require('q'); | |
var sum = Q.fcall(function () { | |
return 1; | |
}); | |
var second = function(first){ | |
return first + 2; | |
} | |
var end = function(result) { console.log(result) }; | |
sum.then(second).done(end) |
This file contains hidden or 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
// Using promises to retrieve Weather Data | |
var Q = require('q'); | |
var RestClient = require('restler') | |
var getWeatherFromCity = function(city) { | |
var d = Q.defer(); | |
var url = 'http://api.openweathermap.org/data/2.5/weather?q=' + city; | |
RestClient.get(url).on('complete', function(data){ | |
d.resolve(data.name); | |
}); | |
return d.promise; | |
}; | |
var someFunc = function(data){ | |
var d = Q.defer(); | |
getWeatherFromCity('Sao Paulo').then(function(spData){ | |
d.resolve([data, spData]) | |
}); | |
return d.promise; | |
}; | |
var doneFunc = function(data){ | |
console.log(data); | |
var dateTwo = new Date(); | |
console.log(dateTwo.getTime() - dateOne.getTime()); | |
}; | |
var dateOne = new Date(); | |
getWeatherFromCity('London').then(someFunc).done(doneFunc); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment