[英]Returning angular promise from a function that resolves the promise very quickly
I am writing an asynchronous javascript function that will be called by consumers to get certain data. 我正在编写一个异步的javascript函数,消费者会调用它来获取某些数据。 Following is the simple implementation that I wrote initially (error handing and other stuff removed for clarity).
以下是我最初编写的简单实现(为了清楚起见,错误处理和其他内容被删除)。
function getData(callback){
if (data is available as a JavaScript object){
callback(data);
}else{
getAsyncData(function(data){
//some transformations on data
callback(data);
});
}
}
What is important to note is that getData can return data quickly if data is already available as a JavaScript object. 需要注意的重要一点是,如果数据已作为JavaScript对象可用,则getData可以快速返回数据。
I want to replace this implementation with the one that returns a promise object to the caller. 我想将此实现替换为将一个promise对象返回给调用者的实现。 This fiddle shows sample implementation - http://fiddle.jshell.net/ZjUg3/44/
这个小提琴显示了示例实现 - http://fiddle.jshell.net/ZjUg3/44/
The question - Since getData can return quickly, can there be a possiblity where getData is resolving the promise even before caller has established handler chain using then method? 问题 - 因为getData可以快速返回,即使在调用者使用then方法建立处理程序链之前,getData是否有可能解析 promise? Just to simulate this, in the fiddle if i call then method inside setTimeout function (with zero delay), callback doesn't get called.
只是为了模拟这个,如果我在setTimeout函数中调用then方法(零延迟),则不会调用回调函数。 If i call the then method outside of the setTimeout function, callback gets called.
如果我在setTimeout函数之外调用then方法,则调用callback。 I am not sure if this is even a valid concern or valid usecase.
我不确定这是否是一个有效的关注或有效的用例。 I am quite new to angularjs development and would appreciate your views :)
我是angularjs开发的新手,非常感谢你的观点:)
If you want getData()
to return a $q
promise instead of using a callback, I'd do the following refactor using $q.when()
and usual $q.resolve()
: 如果你想让
getData()
返回一个$q
promise而不是使用回调,我会使用$q.when()
和通常的$q.resolve()
来执行以下重构:
function getData()
{
if (data is available as a JavaScript object) {
return $q.when(data); // resolves immediately
} else {
var q = $q.defer();
getAsyncData(function(data){
//some transformations on data
q.resolve(data);
});
return q.promise;
}
}
No, a significant and important part of being a promise is that it doesn't matter when you attach the handler. 不,作为承诺的一个重要而重要的部分是,当您附加处理程序时无关紧要。 Even if you create a promise now and resolve it immediately, then keep your computer running for the next 50 years, then attach a handler it will still fire.
即使您现在创建了一个承诺并立即解决它,然后让您的计算机在接下来的50年内继续运行,然后附加一个它仍将触发的处理程序。
All of this does assume that there isn't a bug/corner case in angularjs's promise implementation. 所有这一切都假设angularjs的promise实现中没有bug / corner案例。 If it doesn't work, it's a bug though.
如果它不起作用,那它就是一个bug。
If you ever need to know anything about how promises work, you can always refer to the Promises/A+ spec which angular adheers to. 如果您需要了解Promises如何工作,您可以随时参考Promises / A +规范哪个角度adheers。 As a spec, it's one of the simplest and easiest to understand that I've come across (although I should mention that I've been involved in the spec for quite a while now).
作为一个规范,它是我遇到的最简单和最容易理解的一个(虽然我应该提到我已经参与了规范很长一段时间了)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.