如何简化Q承诺示例 [英] How to simplify Q promise example
问题描述
我正在开发一个简单的应用程序,该应用程序进行连续的
ajax调用,并将第一次调用的结果传递给下一个。
I was working on a simple application that makes sequential ajax calls, passing result of first call into the next one.
我当然不不想进入回调地狱
并因此查看 Promises / A + 规范示例和 Q库。
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.
现在我要看两件事:
- 简化承诺顺序的方式(在我的情况下,这取决于一个
) -
建议
- 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: []
*/
谢谢大家!
推荐答案
您可以通过组合两个变量来简化循环:
You can simplify your loop by combining the two variables:
var nextTick = getDeferredResult();
for (var i = modifyableObject.toProcess.length; i > 1; i --) {
nextTick = nextTick.then(getDeferredResult);
}
或者,
return modifyableObject.toProcess.reduce(function(promise, item) {
return promise.then(getDeferredResult);
}, Q.resolve());
您也可以简化功能:
You can simplify your function too:
return Q.delay(200).then(function) {
return "..." + modifyableObject.toProcess.shift();
});
jQuery AJAX还返回一个承诺,该承诺与Q兼容(在最新版本的jQuery中)
jQuery AJAX also returns a promise, which Q is compatible with (in recent versions of jQuery)
然后可以通过将每个项目传递给函数来组合这两项改进:
You can then combine the two improvements by passing each item to the function:
return modifyableObject.toProcess.reduce(function(promise, item) {
return promise.then(processItem.bind(null, item));
}, Q.resolve());
function processItem(item) {
return Q.delay(200).then(function) {
return "..." + modifyableObject.toProcess.shift();
});
}
这篇关于如何简化Q承诺示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!