网络API异步方法AngularJS [英] Web API async method with AngularJS
问题描述
我是新来使用 AngularJS
与 MVC
5,我一直在考虑使用的Web API
与 AngularJS
,因为它似乎是一个很好的解决方案将数据加载到您的客户端模式。
I'm new to using AngularJS
with MVC
5 and I've been looking at using Web API
with AngularJS
since it seems like a good solution for loading data into your client side models.
不过,我注意到,不少导游使用返回异步操作任务<型号>
,我不明白什么好处,这给你比只使用标准的Web API
的操作(例如:<一href="http://monox.mono-software.com/blog/post/Mono/233/Async-upload-using-angular-file-upload-directive-and-net-WebAPI-service/" rel="nofollow">http://monox.mono-software.com/blog/post/Mono/233/Async-upload-using-angular-file-upload-directive-and-net-WebAPI-service/和<一href="http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/build-a-single-page-application-%28spa%29-with-aspnet-web-api-and-angularjs" rel="nofollow">http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/build-a-single-page-application-%28spa%29-with-aspnet-web-api-and-angularjs).
However I've noticed that quite a few guides use async actions that returns Task<Model>
and I don't understand what benefit this gives you over using just standard Web API
actions (examples: http://monox.mono-software.com/blog/post/Mono/233/Async-upload-using-angular-file-upload-directive-and-net-WebAPI-service/ and http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/build-a-single-page-application-%28spa%29-with-aspnet-web-api-and-angularjs).
由于这些调用的Web API是异步反正我不知道为什么我们需要让这些方法调用异步的。那岂不是更好地只使用标准的Web API调用?
Since these calls to Web API are asynchronous anyway I don't know why we need to make these method calls asynchronous. Wouldn't it be better to use just standard Web API calls?
我不知道,如果计算器是正确的地方,但我所期待的,为什么呼叫做这样一个解释。
I don't know if stackoverflow is the right place for this but I was hoping for an explanation on why calls are done this way.
推荐答案
的的好处异步等待
在服务器上的可扩展性和性能。当使用异步(非阻塞)换上同步(阻塞)操作你腾出是previously阻塞等待,你可以用它来同时处理其他请求的线程。
The benefit of async-await
on the server is scalability and performance. When you replace synchronous (blocking) operations with asynchronous (non-blocking) ones you free up threads that were previously blocked waiting which you can use to handle other requests concurrently.
异步计谋
使您能够以更少的资源做更多的。
async-await
enables you to do more with less resources.
让我们假设有这种同步方式:
Let's assume we have this synchronous method:
public void Process()
{
for (int i = 0; i < 1000; i++)
{
Math.Sqrt(i);
}
Thread.Sleep(3000); // sync I/O
}
通过使异步
:
public async Task ProcessAsync()
{
for (int i = 0; i < 1000; i++)
{
Math.Sqrt(i);
}
await Task.Delay(3000) // async I/O
}
我们可以使用一个线程来处理相同的方法的多个请求,因为该线程处理该请求被释放,当你等待
的异步操作。
We can use a single thread to process multiple requests of the same method because the thread handling the request is freed up when you await
the asynchronous operation.
这篇关于网络API异步方法AngularJS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!