I was working on a simple application that makes sequential ajax calls, passing result of first call into the next one.
Of course I don't want to go into the callback hell, and therefore look into Promises/A+ spec example and Q library.
I have prepared an async function that should result in what I want. But I want an insight on how I can simplify the Sequential promise passing.
For now I am still reading on how to best work with promises and deferred objects, so forgive me for the very naive code.
So now I am looking at two things:
- the way to simplify the sequencing of promises (that depend on one another as in my case)
suggestions
var modifyableObject = { toProcess : ["one", "two", "three", "four", "five"] } function returnsDeferredResults(someResult) { var deferred = Q.defer(); // my async function (setTimeout for now will do, $.ajax() later) setTimeout(function () { var nextResult = (someResult || " Initial_Blank_Value ") + "..." + modifyableObject.toProcess[0]; modifyableObject.toProcess = modifyableObject.toProcess.splice(1); console.log("New Tick Result: ", nextResult, "Array: ", modifyableObject.toProcess); deferred.resolve( nextResult); }, 200); return deferred.promise; } //$("#test_promise").click(function () { function getDeferredResult(prevResult) { return returnsDeferredResults(prevResult); } var prevResult = getDeferredResult(); var nextTick = ""; for (var i = modifyableObject.toProcess.length; i > 1; i --) { if (nextTick) nextTick = nextTick.then(getDeferredResult); else nextTick = prevResult.then(getDeferredResult); } //nextTick.fin(function(){ ...}); //}); /* New Tick Result: Initial_Blank_Value ...one Array: ["two", "three", "four", "five"] New Tick Result: Initial_Blank_Value ...one...two Array: ["three", "four", "five"] New Tick Result: Initial_Blank_Value ...one...two...three Array: ["four", "five"] New Tick Result: Initial_Blank_Value ...one...two...three...four Array: ["five"] New Tick Result: Initial_Blank_Value ...one...two...three...four...five Array: [] */
Thank you everyone in advance!