AngularJs:有方法的返回同步时,它调用$ http或$内部资源 [英] AngularJs: Have method return synchronously when it calls $http or $resource internally

查看:122
本文介绍了AngularJs:有方法的返回同步时,它调用$ http或$内部资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法来等待一个承诺,让你可以从它那里得到的实际结果,并返回,而不是返回自己的承诺?我在考虑类似于C#如何等待关键字与工作任务的东西。

Is there a way to wait on a promise so that you can get the actual result from it and return that instead of returning the promise itself? I'm thinking of something similar to how the C# await keyword works with Tasks.

下面是这样,它可以在一个if语句中使用返回true或false,而不是一个承诺)为什么我想有一个像canAccess的方法(一个例子。该方法canAccess()将用做一个AJAX调用$ http或$资源,然后以某种方式等待承诺得到解决。

Here is an example of why I'd like to have a method like canAccess() that returns true or false instead of a promise so that it can be used in an if statement. The method canAccess() would make an AJAX call using $http or $resource and then somehow wait for the promise to get resolved.

该会是这个样子:

$scope.canAccess = function(page) {
    var resource = $resource('/api/access/:page');
    var result = resource.get({page: page});

    // how to await this and not return the promise but the real value
    return result.canAccess;
}

反正有做到这一点?

Is there anyway to do this?

感谢。

推荐答案

在普遍认为是一个坏主意。让我来告诉你为什么。的JavaScript在浏览器基本上是一个单线程的野兽。试想想它,它在Node.js的单线程了。所以,什么你做不回在你开始等待远程请求成功或失败将可能涉及某种循环的请求后延迟code的执行点。事情是这样的:

In general that's a bad idea. Let me tell you why. JavaScript in a browser is basically a single threaded beast. Come to think of it, it's single threaded in Node.js too. So anything you do to not "return" at the point you start waiting for the remote request to succeed or fail will likely involve some sort of looping to delay execution of the code after the request. Something like this:

var semaphore = false;
var superImportantInfo = null;

// Make a remote request.
$http.get('some wonderful URL for a service').then(function (results) {
  superImportantInfo = results;

  semaphore = true;
});

while (!semaphore) {
  // We're just waiting.
}

// Code we're trying to avoid running until we know the results of the URL call.
console.log('The thing I want for lunch is... " + superImportantInfo);

但是,如果你试图在浏览器和呼叫需要很长的时间,浏览器会认为你的JavaScript code是停留在一个循环,并在用户的脸上弹出一个消息使用户有机会停止您code。因此JavaScript的结构,它像这样:

But if you try that in a browser and the call takes a long time, the browser will think your JavaScript code is stuck in a loop and pop up a message in the user's face giving the user the chance to stop your code. JavaScript therefore structures it like so:

// Make a remote request.
$http.get('some wonderful URL for a service').then(function (results) {
  // Code we're trying to avoid running until we know the results of the URL call.
  console.log('The thing I want for lunch is... " + results);
});

// Continue on with other code which does not need the super important info or 
// simply end our JavaScript altogether. The code inside the callback will be 
// executed later.

这个想法是,在回调code将事件每当服务调用返回被触发。因为事件驱动是怎样的JavaScript喜欢它。在JavaScript中计时器事件,用户行动事件,HTTP / HTTPS调用发送和接收的数据生成的事件了。而你希望构建你的code,当他们来到这些事件作出回应。

The idea being that the code in the callback will be triggered by an event whenever the service call returns. Because event driven is how JavaScript likes it. Timers in JavaScript are events, user actions are events, HTTP/HTTPS calls to send and receive data generate events too. And you're expected to structure your code to respond to those events when they come.

您无法组织你的code,使得它认为canAccess是假的,直到时间作为远程服务调用返回,它可能发现它毕竟真的是真的吗?我这样做,在AngularJS code所有的时间,我不知道权限的最终集我应该展现给用户的东西是因为我还没收到货或者我还没有收到所有数据到首先在页面显示。我有默认其显示,直到实际数据回来,然后将页调整到新的形式的基础上的新的数据。这两种方式AngularJS的结合使得这真的很简单。

Can you not structure your code such that it thinks canAccess is false until such time as the remote service call returns and it maybe finds out that it really is true after all? I do that all the time in AngularJS code where I don't know what the ultimate set of permissions I should show to the user is because I haven't received them yet or I haven't received all of the data to display in the page at first. I have defaults which show until the real data comes back and then the page adjusts to its new form based on the new data. The two way binding of AngularJS makes that really quite easy.

这篇关于AngularJs:有方法的返回同步时,它调用$ http或$内部资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆