延期,承诺和期货有什么区别?
这三者背后是否有普遍认可的理论?
我看到了一些使用promises访问FB Graph API 的Facebook登录服务的例子.
示例#1:
this.api = function(item) {
var deferred = $q.defer();
if (item) {
facebook.FB.api('/' + item, function (result) {
$rootScope.$apply(function () {
if (angular.isUndefined(result.error)) {
deferred.resolve(result);
} else {
deferred.reject(result.error);
}
});
});
}
return deferred.promise;
}
Run Code Online (Sandbox Code Playgroud)
"$scope.$digest() // Manual scope evaluation"获得响应时使用的服务
示例#2:
angular.module('HomePageModule', []).factory('facebookConnect', function() {
return new function() {
this.askFacebookForAuthentication = function(fail, success) {
FB.login(function(response) {
if (response.authResponse) {
FB.api('/me', success);
} else {
fail('User cancelled login or did not …Run Code Online (Sandbox Code Playgroud) 有人可以解释一下$q.whenAngularJS的工作原理吗?我正在尝试分析$http工作方式,并发现了这个:
var promise = $q.when(config);
Run Code Online (Sandbox Code Playgroud)
这是来自Chrome控制台的配置对象:
Object {transformRequest: Array[1], transformResponse: Array[1], cache: Object, method: "GET", url: "/schedule/month_index.html"…}
cache: Object
headers: Object
method: "GET"
transformRequest: Array[1]
transformResponse: Array[1]
url: "/schedule/month_index.html"
__proto__: Object
Run Code Online (Sandbox Code Playgroud)
接下来发生什么?该对象如何被解决或拒绝?
在我的Angular.js应用程序中,我正在运行一些异步操作.在它开始之前我用模态div覆盖应用程序,然后一旦操作完成,我需要删除div,无论操作是否成功.
目前我有这个:
LoadingOverlay.start();
Auth.initialize().then(function() {
LoadingOverlay.stop();
}, function() {
LoadingOverlay.stop(); // Code needs to be duplicated here
})
Run Code Online (Sandbox Code Playgroud)
它运作良好,但我更喜欢像这个伪代码一样更清洁:
LoadingOverlay.start();
Auth.initialize().finally(function() { // *pseudo-code* - some function that is always executed on both failure and success.
LoadingOverlay.stop();
})
Run Code Online (Sandbox Code Playgroud)
我想这是一个相当普遍的问题,所以我想这是可以做到,但不能找到文档中任何事情.有什么想法可以吗?
我希望有一个for循环,每次迭代都会调用异步函数.
在for循环之后,我想执行另一个代码块,但是在for循环中所有先前的调用都已解决之前.
我现在的问题是,在所有异步调用完成之前执行for循环之后的代码块或者根本不执行它.
带有FOR循环的代码部分和后面的代码块(完整代码,请参见小提琴):
[..]
function outerFunction($q, $scope) {
var defer = $q.defer();
readSome($q,$scope).then(function() {
var promise = writeSome($q, $scope.testArray[0])
for (var i=1; i < $scope.testArray.length; i++) {
promise = promise.then(
angular.bind(null, writeSome, $q, $scope.testArray[i])
);
}
// this must not be called before all calls in for-loop have finished
promise = promise.then(function() {
return writeSome($q, "finish").then(function() {
console.log("resolve");
// resolving here after everything has been done, yey!
defer.resolve();
});
});
});
return defer.promise;
}
Run Code Online (Sandbox Code Playgroud)
我创建了一个jsFiddle,可以在这里找到http://jsfiddle.net/riemersebastian/B43u6/3/. …
我需要以同步方式进行三次HTTP调用,如何将数据从一个调用传递到另一个调用?
function first()
{
ajax()
}
function second()
{
ajax()
}
function third()
{
ajax()
}
function main()
{
first().then(second).then(third)
}
Run Code Online (Sandbox Code Playgroud)
我试图将延迟用于两个函数,我想出了一个部分解决方案.我可以将它扩展为三个功能吗?
function first() {
var deferred = $.Deferred();
$.ajax({
"success": function (resp)
{
deferred.resolve(resp);
},
});
return deferred.promise();
}
function second(foo) {
$.ajax({
"success": function (resp)
{
},
"error": function (resp)
{
}
});
}
first().then(function(foo){second(foo)})
Run Code Online (Sandbox Code Playgroud) 我需要用来defer释放使用C库手动创建的分配,但我还需要os.Exit在某些时候使用非0状态.棘手的部分是os.Exit跳过任何延迟指令:
package main
import "fmt"
import "os"
func main() {
// `defer`s will _not_ be run when using `os.Exit`, so
// this `fmt.Println` will never be called.
defer fmt.Println("!")
// sometimes ones might use defer to do critical operations
// like close a database, remove a lock or free memory
// Exit with status code.
os.Exit(3)
}
Run Code Online (Sandbox Code Playgroud)
游乐场:从https://gobyexample.com/exit窃取http://play.golang.org/p/CDiAh9SXRM
那么如何退出一个尊重已宣布defer呼叫的go计划呢?还有其他选择os.Exit吗?
我觉得拥有一个承诺的 JavaScript变量的命名约定会很有用.我一般不喜欢或提倡超出编程语言标准的命名约定,但是在编程风格中,promises作为函数参数传递,通常难以一目了然地知道变量是持有承诺还是"真实的东西".
我个人使用promiseOfFoo和pFoo,但我觉得前者有点冗长,而后者给我倒叙从匈牙利.
有一个常用的惯例吗?
我使用延迟模块在Node.js中创建了我的第一个延迟对象,当我将结果传递给下一个函数并触发解析和拒绝时它很有效.当每个函数返回deferred.promise时,如何链接函数数组的执行?我喜欢第一个函数的输入参数数组和输入参数,以及前一个函数的每个下一个函数get参数.
它f1(100).then(f2).then(f3)有点像,但是当我有多个函数时.
我需要在不锁定浏览器的情况下发出一系列N ajax请求,并希望使用jquery延迟对象来完成此任务.
下面是三个请求一个简单的例子,但我的程序可能需要超过100个排队(注意,这是不准确的使用情况下,实际的代码确实需要确保步骤执行下一之前成功(N-1)步):
$(document).ready(function(){
var deferred = $.Deferred();
var countries = ["US", "CA", "MX"];
$.each(countries, function(index, country){
deferred.pipe(getData(country));
});
});
function getData(country){
var data = {
"country": country
};
console.log("Making request for [" + country + "]");
return $.ajax({
type: "POST",
url: "ajax.jsp",
data: data,
dataType: "JSON",
success: function(){
console.log("Successful request for [" + country + "]");
}
});
}
Run Code Online (Sandbox Code Playgroud)
这是写入控制台的内容(所有请求都是并行进行的,响应时间与每个国家/地区的数据大小成正比:
Making request for [US]
Making request for [CA]
Making request for [MX]
Successful request for [MX]
Successful request for …Run Code Online (Sandbox Code Playgroud)