0

I'm braving the warning of similar questions, because the ones I see are not matching or have few or no answers, and hope that if I'm specific enough the problem with .then results in .NET6 MVC can be pinpointed. Many answers on SO are also outdated in 2022, and this is as frustrating as information on the web, and extreme lacks in official documentation on this subject.

Common Application

processIt(id).then((data) => refreshDiv(data));

Problem: The .then solution described in this popular question says any function with any named parameter after the => will return the result as that parameter. This is how I wrote it, and it seems to return the promise data instead of the result. (Responded == false, see below.)

processIt

async function processIt(data) {
    let result;
    try {
        result = await $.ajax({
            type: "POST",
            url: "ProcessRow",
            data: { "Id": data }
        });
    } catch (error) {
        console.error(error);
    }
    return result;
}

ProcessRow

public async Task<ActionResult> ProcessRow([FromForm] string id)

This C# method waits for the Db row to indeed update. When a human (me) checks the Db, the row is indeed updated.

do
{
    await Task.Run(() =>
    {
        var responded = rows.Where(row => row.Responded)...Any()... //pseudocode
    });

} while (!responded);

if (responded)
    return Json(rows.First());

refreshDiv

In refreshDiv() however, data.Responded is false. In my mind, if .then returns the result, a false value cannot pass.

I've reduced this to the essentials (not least to find the error while formulating the question, which is a great thing!), and I've tried some other answers on here such as "data.result" from here (which when measured has a type of === undefined).

I've made this question as clear and concise as I can, hoping that we can find an equally specific answer to this "look up a thing and refresh a div" that is so easy in other environments.

Comment and I'll be happy to improve the question if I can! :)

Henrik Erlandsson
  • 3,797
  • 5
  • 43
  • 63
  • I'm maybe missing it, but what's the actual question? – Keith Aug 03 '22 at 17:09
  • Thx Keith, the question is stated in the title, and under Problem:. It applies to the tags and year as always. Speaking of, I should specify .NET version! – Henrik Erlandsson Aug 03 '22 at 17:15
  • 1
    @HenrikErlandsson: you should definitely consult a HTTP debugger to see what actual data is passed between the server and the client. From your question it looks like you've missed that part and you are not sure whether the issue is at the client or at the server. Thus, you include both in your question, making it also difficult for us to help. However, just checking what is returned from the server, you would immediately know whether the probiem is at the server or at the client and thus, your'd edit your question to only include relevant information. – Wiktor Zychla Aug 03 '22 at 17:24
  • Also, what's the point of `await Task.Run(` to execute something that looks like a synchronous code? Kind of looks like an antipattern (there could be something more behind it but without a context it looks confusing) – Wiktor Zychla Aug 03 '22 at 17:29
  • Thx Wiktor. The debugger returns the row with Responded == false, whereas I do not send a row, I just send an Id. From this, .then does return the result. How the server thread is able to return a result with Responded == false is unknown, since I don't really return the row either, but a new JsonResult with its fields. Can using the same DbContext for creating and reading a row result in reading the modified row, but when Json() reads the modified row it somehow reads the old unmodified row? – Henrik Erlandsson Aug 04 '22 at 11:46
  • This was it. Using a new DbContext for the polling transferred the updated row into the JsonResult. No other code changed. Something behind the scenes still reports updated row field values if you don't, but IQueryable.First() won't pass those updated values. – Henrik Erlandsson Aug 04 '22 at 12:11

0 Answers0