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! :)