P
P
Planet_932019-02-07 12:15:07
JavaScript
Planet_93, 2019-02-07 12:15:07

How to implement asynchronous request execution in ASP .NET MVC 5?

Good afternoon!

With the help of a timer, every second I send a request and get a test value. And in parallel to it I send another request, which I receive in 10 seconds.

Client side:

var timerId;

        function startTimer() {
            timerId = setInterval(() => {
                //Запрос на получение значения прогресса
                axios.post("/Uploader/GetProgress", null).then(resp => {
                    console.log(resp.data);
                });
            }, 1000);
            
            axios.post("/Uploader/AcceptForecast", null).then(resp => {
                console.log(resp.data.Message);
            });

        }


Server (ASP.NET C#) :
public class UploaderController : Controller{
private static int itr = 0;
        public JsonResult AcceptForecast()
        {
//Задержка 10 сек
            for (int i = 0; i < 10; i++)
            {
                Thread.Sleep(1000);
            }
            return Json(new { Message = "Сообщение отправлено" });
        }

        [HttpPost]
        public JsonResult GetProgress()
        {
            itr++;
            return Json(itr);
        }
}


The result is the following result, that the requests in the timer wait until the callback from the request '/Uploader/AcceptForecast' returns. Can

5c5bf570507ba605660025.jpeg

you please tell me how to make the requests run in parallel to each other?

The real problem is that I will write data to the database and want to get the progress of the write.

If you do not send a request for progress, then everything works fine.
var timerId;
        var itr = 0;
        function startTimer() {
            timerId = setInterval(() => {
                itr++;
                console.log(itr);
            }, 1000);
            
            axios.post("/Uploader/AcceptForecast", null).then(resp => {
                console.log(resp.data.Message);
            });
        }
        startTimer();

Result:
5c5bfd1c0366b019443526.jpeg

It was found that requests are sent asynchronously on the client, and they are sent in turn from the server.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Planet_93, 2019-02-12
@Planet_93

Found the answer to my problem. On the server, if requests came during one session, then they queue up.
Details: tech-journals.com/jonow/2011/10/22/the-downsides-o...

A
Andrey Okhotnikov, 2019-02-07
@tsepen

You can use axios all to make multiple requests at once. Example

function getUserAccount() {
  return axios.get('/user/12345');
}

function getUserPermissions() {
  return axios.get('/user/12345/permissions');
}

axios.all([getUserAccount(), getUserPermissions()])
  .then(axios.spread(function (acct, perms) {
    // Both requests are now complete
  }));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question