I'm new to Promises and I don't know the best practises. The following code retrieves page history then search history. I want this history to be saved in variables in order to use it later. I wrote this code but I don't know if it's the best approach. Can you please give me your opinion?
/** Let's analyse page&search history */
var history = {};
var savePage = function(obj)
{
var defer = Q.defer();
history.page = obj;
defer.resolve();
return defer.promise;
}
var saveSearch = function(obj)
{
var defer = Q.defer();
history.search = obj;
defer.resolve();
return defer.promise;
}
var displayHistory = function()
{
console.log(history);
}
/** Promises chain */
historyLib.get_entries_of(uid, "page")
.then(savePage, console.log)
.then(historyLib.get_entries_of(uid, "search"))
.then(saveSearch, console.log)
.then(displayHistory);
get_entries_of
; it's being called immediately, and returning a promise (as far as I can tell), neither of which is what I think you're going for. – Flambino Jun 14 at 15:48