-2

I need to write function which prepare me a HTML to inuput into my site, I tried like this

function prepareHTML(valId){
  $.ajax({
    type: "POST",
    url: "SOAPulr_getData",
    data: {operationId: valId}
  })
  .done(function(result, status, jqXHR){
    var def = $.Deferred();
    var response = jqXHR.responseText;
    var outputHTML;
    [... here i generate HTML code ...]
    def.resolve(outputHTML);
    return def.promise();
  });
}

now when i tried to run this function:

prepareHTML(22)
.done(function(data){
  console.log(data)
});

I get an error: TypeError: prepareHTML(...) is undefined when I run prepareHTML(22) there is no error in console.

What I do wrong?

--

Best regards

ssnake

ssnake
  • 365
  • 2
  • 14
  • Does this answer your question? [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Klaycon Mar 30 '20 at 21:21

2 Answers2

0

prepareHTML has no return statement so it returns undefined

return $.ajax...
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • This is basically the answer. Also, remove the `.done()` entirely since nothing in there is useful once you're return the promise that `$.ajax()` already makes. – jfriend00 Mar 30 '20 at 21:21
  • am I going crazy? `return`ing the `.ajax().done()` call will not cause it to return a promise, as the callback's return value is not obtained synchronously... – Klaycon Mar 30 '20 at 21:23
  • @Klaycon - Yes, that's why you have to get rid of the `.done()` or use `.then()`. `.done()` is an old, jQuery-specific, non-standard way of doing promises. No reason I know of to ever use it now that `.then()` and `.catch()` exist in a standard implementation. But, the OP's question can remove the `.done()` entirely because there's nothing useful in there at all. – jfriend00 Mar 30 '20 at 21:24
  • @jfriend00 if you have `return $.ajax(...).done(...)` the value returned by the function will be of type `Deferred` and it will refer to the original `$.ajax()` call, not the `return def.promise()` the asker wants to reference. – Klaycon Mar 30 '20 at 21:25
  • @Klaycon - The OP doesn't need their existing `.done()` at all. All it was doing was trying to manufacture a promise they don't even need. – jfriend00 Mar 30 '20 at 21:26
  • @jfriend00 ah, I see what you mean now - I think I got confused as there were two `.done()`s and I thought you meant only the one in the asker's second snippet. right, my mistake! in actuality both of them should be replaced/removed – Klaycon Mar 30 '20 at 21:29
  • @Klaycon - Ahh yes. I was only looking at the `$.ajax().done()` - that `.done()`. Calling `prepareHTML()` does have to use something to get the result (preferably `.then()` or `await`). – jfriend00 Mar 30 '20 at 21:34
0

Yes I can do this like that:

function prepareHTML(valId){
  res = $.ajax({
    type: "POST",
    url: "SOAPulr_getData",
    data: {operationId: valId}
  });

  return res.promise();
}

and it works but then I must to generate my HTML into done function:

prepareHTML(22).done(function(result, status, jqXHR){
    var response = jqXHR.responseText;
    [... here i generate HTML code ...]
});

Question is: is it possible to pre-process the respone to have HTML ready in the done function

I need something like this:

prepareHTML(22).done(function(myHTML){
    // here I need ready HTML code prepared into prepareHTML() function
    $("#myId").html(myHTML);
});
ssnake
  • 365
  • 2
  • 14
  • You really ought to stop using `.done()` and use industry standard `.then()`. That makes the flow more compatible with other promises that might be involved in a promise chain. You can literally just drop `.done()` entirely. – jfriend00 Mar 30 '20 at 22:02