returned value from AJAX - Bug or Feature?











up vote
0
down vote

favorite












For a long time, I wondered whether one could get data out of the AJAX callback, but the internet said no.
Now, a colleague of mine seems to have found a way to do so, but I wonder whether this is a bug or a feature?



I wrote a little ES7 testcode working inside our tool to confirm it, it looks like this:






  async function testWrapper(){
var test = await testPromiseResolve()
console.log("async function was resolved to", test)
}

function testPromiseResolve(){
return $.post('../include/Stornieren/retrieveUserID.php', {
//nothing to transmit
}).then((data) => {
data = JSON.parse(data)
console.log("data from testajax is", data)
return data
})





both console logs return the data correctly (here, just a user ID fetched from the database). So the data must have been extracted from inside the promise returned by the jqueryAJAX $.post.



I want to know whether this behavior is a bug or a feature because if its a bug, I can't really use it inside our code since the day may come where this bug is fixed and then the whole thing comes crashing down on me ^^



Also, I couldn't really test whether this code keeps the functionality of async/await. When using return inside promises (I think I should use resolve/reject instead but I must admit that I haven't fully understood how these work inside jqueryAJAX) I might knock out the promise functionality and therefore, when the code really NEEDS to wait for the completion of an AJAX before proceeding, this might pose a risk for the future reliability of the code when database interactions might take longer etc..










share|improve this question






















  • What do you mean by "data must have been extracted from inside the promise"? Yes, that's exactly what await is supposed to do; it works just like .then().
    – Bergi
    Nov 21 at 10:38










  • I read in numerous answers on stackoverflow that returning a value from a promise shall be impossible. Basically "what happens in callback, stays in callback" (although we dont call this callback anymore^^). Await expects a promise and once its been delivered, code execution moves on. I even had posted questions here where I was told again and again that a value cant be returned from "inside the promise". Sry if its hard to follow what I mean, but I also find it hard to explain it^^
    – JSONBUG123
    Nov 21 at 11:06










  • The whole code that runs after an await expression is conceptually in a .then() callback. That's why an async function call must return a promise, it can't return the result immediately either.
    – Bergi
    Nov 21 at 14:35










  • ok, but DOES it return the result at all? I mean it does here, somehow, but I wonder whether it actually works like this: "ES7 synchronization functionality + returning the result from the then callback" or if the syntax above just breaks the whole async/await and promise functionality?
    – JSONBUG123
    Nov 21 at 14:50










  • No, the call to testWrapper() in your example does not return the result. It just creates a promise, and will log the testPromiseResolve() result later. I does not stop execution, it does not break anything. What you see there is exactly the async/await functionality: it can stop (and defer) the execution inside the async function body only.
    – Bergi
    Nov 21 at 14:53















up vote
0
down vote

favorite












For a long time, I wondered whether one could get data out of the AJAX callback, but the internet said no.
Now, a colleague of mine seems to have found a way to do so, but I wonder whether this is a bug or a feature?



I wrote a little ES7 testcode working inside our tool to confirm it, it looks like this:






  async function testWrapper(){
var test = await testPromiseResolve()
console.log("async function was resolved to", test)
}

function testPromiseResolve(){
return $.post('../include/Stornieren/retrieveUserID.php', {
//nothing to transmit
}).then((data) => {
data = JSON.parse(data)
console.log("data from testajax is", data)
return data
})





both console logs return the data correctly (here, just a user ID fetched from the database). So the data must have been extracted from inside the promise returned by the jqueryAJAX $.post.



I want to know whether this behavior is a bug or a feature because if its a bug, I can't really use it inside our code since the day may come where this bug is fixed and then the whole thing comes crashing down on me ^^



Also, I couldn't really test whether this code keeps the functionality of async/await. When using return inside promises (I think I should use resolve/reject instead but I must admit that I haven't fully understood how these work inside jqueryAJAX) I might knock out the promise functionality and therefore, when the code really NEEDS to wait for the completion of an AJAX before proceeding, this might pose a risk for the future reliability of the code when database interactions might take longer etc..










share|improve this question






















  • What do you mean by "data must have been extracted from inside the promise"? Yes, that's exactly what await is supposed to do; it works just like .then().
    – Bergi
    Nov 21 at 10:38










  • I read in numerous answers on stackoverflow that returning a value from a promise shall be impossible. Basically "what happens in callback, stays in callback" (although we dont call this callback anymore^^). Await expects a promise and once its been delivered, code execution moves on. I even had posted questions here where I was told again and again that a value cant be returned from "inside the promise". Sry if its hard to follow what I mean, but I also find it hard to explain it^^
    – JSONBUG123
    Nov 21 at 11:06










  • The whole code that runs after an await expression is conceptually in a .then() callback. That's why an async function call must return a promise, it can't return the result immediately either.
    – Bergi
    Nov 21 at 14:35










  • ok, but DOES it return the result at all? I mean it does here, somehow, but I wonder whether it actually works like this: "ES7 synchronization functionality + returning the result from the then callback" or if the syntax above just breaks the whole async/await and promise functionality?
    – JSONBUG123
    Nov 21 at 14:50










  • No, the call to testWrapper() in your example does not return the result. It just creates a promise, and will log the testPromiseResolve() result later. I does not stop execution, it does not break anything. What you see there is exactly the async/await functionality: it can stop (and defer) the execution inside the async function body only.
    – Bergi
    Nov 21 at 14:53













up vote
0
down vote

favorite









up vote
0
down vote

favorite











For a long time, I wondered whether one could get data out of the AJAX callback, but the internet said no.
Now, a colleague of mine seems to have found a way to do so, but I wonder whether this is a bug or a feature?



I wrote a little ES7 testcode working inside our tool to confirm it, it looks like this:






  async function testWrapper(){
var test = await testPromiseResolve()
console.log("async function was resolved to", test)
}

function testPromiseResolve(){
return $.post('../include/Stornieren/retrieveUserID.php', {
//nothing to transmit
}).then((data) => {
data = JSON.parse(data)
console.log("data from testajax is", data)
return data
})





both console logs return the data correctly (here, just a user ID fetched from the database). So the data must have been extracted from inside the promise returned by the jqueryAJAX $.post.



I want to know whether this behavior is a bug or a feature because if its a bug, I can't really use it inside our code since the day may come where this bug is fixed and then the whole thing comes crashing down on me ^^



Also, I couldn't really test whether this code keeps the functionality of async/await. When using return inside promises (I think I should use resolve/reject instead but I must admit that I haven't fully understood how these work inside jqueryAJAX) I might knock out the promise functionality and therefore, when the code really NEEDS to wait for the completion of an AJAX before proceeding, this might pose a risk for the future reliability of the code when database interactions might take longer etc..










share|improve this question













For a long time, I wondered whether one could get data out of the AJAX callback, but the internet said no.
Now, a colleague of mine seems to have found a way to do so, but I wonder whether this is a bug or a feature?



I wrote a little ES7 testcode working inside our tool to confirm it, it looks like this:






  async function testWrapper(){
var test = await testPromiseResolve()
console.log("async function was resolved to", test)
}

function testPromiseResolve(){
return $.post('../include/Stornieren/retrieveUserID.php', {
//nothing to transmit
}).then((data) => {
data = JSON.parse(data)
console.log("data from testajax is", data)
return data
})





both console logs return the data correctly (here, just a user ID fetched from the database). So the data must have been extracted from inside the promise returned by the jqueryAJAX $.post.



I want to know whether this behavior is a bug or a feature because if its a bug, I can't really use it inside our code since the day may come where this bug is fixed and then the whole thing comes crashing down on me ^^



Also, I couldn't really test whether this code keeps the functionality of async/await. When using return inside promises (I think I should use resolve/reject instead but I must admit that I haven't fully understood how these work inside jqueryAJAX) I might knock out the promise functionality and therefore, when the code really NEEDS to wait for the completion of an AJAX before proceeding, this might pose a risk for the future reliability of the code when database interactions might take longer etc..






  async function testWrapper(){
var test = await testPromiseResolve()
console.log("async function was resolved to", test)
}

function testPromiseResolve(){
return $.post('../include/Stornieren/retrieveUserID.php', {
//nothing to transmit
}).then((data) => {
data = JSON.parse(data)
console.log("data from testajax is", data)
return data
})





  async function testWrapper(){
var test = await testPromiseResolve()
console.log("async function was resolved to", test)
}

function testPromiseResolve(){
return $.post('../include/Stornieren/retrieveUserID.php', {
//nothing to transmit
}).then((data) => {
data = JSON.parse(data)
console.log("data from testajax is", data)
return data
})






ajax async-await ecmascript-7






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 at 8:27









JSONBUG123

487




487












  • What do you mean by "data must have been extracted from inside the promise"? Yes, that's exactly what await is supposed to do; it works just like .then().
    – Bergi
    Nov 21 at 10:38










  • I read in numerous answers on stackoverflow that returning a value from a promise shall be impossible. Basically "what happens in callback, stays in callback" (although we dont call this callback anymore^^). Await expects a promise and once its been delivered, code execution moves on. I even had posted questions here where I was told again and again that a value cant be returned from "inside the promise". Sry if its hard to follow what I mean, but I also find it hard to explain it^^
    – JSONBUG123
    Nov 21 at 11:06










  • The whole code that runs after an await expression is conceptually in a .then() callback. That's why an async function call must return a promise, it can't return the result immediately either.
    – Bergi
    Nov 21 at 14:35










  • ok, but DOES it return the result at all? I mean it does here, somehow, but I wonder whether it actually works like this: "ES7 synchronization functionality + returning the result from the then callback" or if the syntax above just breaks the whole async/await and promise functionality?
    – JSONBUG123
    Nov 21 at 14:50










  • No, the call to testWrapper() in your example does not return the result. It just creates a promise, and will log the testPromiseResolve() result later. I does not stop execution, it does not break anything. What you see there is exactly the async/await functionality: it can stop (and defer) the execution inside the async function body only.
    – Bergi
    Nov 21 at 14:53


















  • What do you mean by "data must have been extracted from inside the promise"? Yes, that's exactly what await is supposed to do; it works just like .then().
    – Bergi
    Nov 21 at 10:38










  • I read in numerous answers on stackoverflow that returning a value from a promise shall be impossible. Basically "what happens in callback, stays in callback" (although we dont call this callback anymore^^). Await expects a promise and once its been delivered, code execution moves on. I even had posted questions here where I was told again and again that a value cant be returned from "inside the promise". Sry if its hard to follow what I mean, but I also find it hard to explain it^^
    – JSONBUG123
    Nov 21 at 11:06










  • The whole code that runs after an await expression is conceptually in a .then() callback. That's why an async function call must return a promise, it can't return the result immediately either.
    – Bergi
    Nov 21 at 14:35










  • ok, but DOES it return the result at all? I mean it does here, somehow, but I wonder whether it actually works like this: "ES7 synchronization functionality + returning the result from the then callback" or if the syntax above just breaks the whole async/await and promise functionality?
    – JSONBUG123
    Nov 21 at 14:50










  • No, the call to testWrapper() in your example does not return the result. It just creates a promise, and will log the testPromiseResolve() result later. I does not stop execution, it does not break anything. What you see there is exactly the async/await functionality: it can stop (and defer) the execution inside the async function body only.
    – Bergi
    Nov 21 at 14:53
















What do you mean by "data must have been extracted from inside the promise"? Yes, that's exactly what await is supposed to do; it works just like .then().
– Bergi
Nov 21 at 10:38




What do you mean by "data must have been extracted from inside the promise"? Yes, that's exactly what await is supposed to do; it works just like .then().
– Bergi
Nov 21 at 10:38












I read in numerous answers on stackoverflow that returning a value from a promise shall be impossible. Basically "what happens in callback, stays in callback" (although we dont call this callback anymore^^). Await expects a promise and once its been delivered, code execution moves on. I even had posted questions here where I was told again and again that a value cant be returned from "inside the promise". Sry if its hard to follow what I mean, but I also find it hard to explain it^^
– JSONBUG123
Nov 21 at 11:06




I read in numerous answers on stackoverflow that returning a value from a promise shall be impossible. Basically "what happens in callback, stays in callback" (although we dont call this callback anymore^^). Await expects a promise and once its been delivered, code execution moves on. I even had posted questions here where I was told again and again that a value cant be returned from "inside the promise". Sry if its hard to follow what I mean, but I also find it hard to explain it^^
– JSONBUG123
Nov 21 at 11:06












The whole code that runs after an await expression is conceptually in a .then() callback. That's why an async function call must return a promise, it can't return the result immediately either.
– Bergi
Nov 21 at 14:35




The whole code that runs after an await expression is conceptually in a .then() callback. That's why an async function call must return a promise, it can't return the result immediately either.
– Bergi
Nov 21 at 14:35












ok, but DOES it return the result at all? I mean it does here, somehow, but I wonder whether it actually works like this: "ES7 synchronization functionality + returning the result from the then callback" or if the syntax above just breaks the whole async/await and promise functionality?
– JSONBUG123
Nov 21 at 14:50




ok, but DOES it return the result at all? I mean it does here, somehow, but I wonder whether it actually works like this: "ES7 synchronization functionality + returning the result from the then callback" or if the syntax above just breaks the whole async/await and promise functionality?
– JSONBUG123
Nov 21 at 14:50












No, the call to testWrapper() in your example does not return the result. It just creates a promise, and will log the testPromiseResolve() result later. I does not stop execution, it does not break anything. What you see there is exactly the async/await functionality: it can stop (and defer) the execution inside the async function body only.
– Bergi
Nov 21 at 14:53




No, the call to testWrapper() in your example does not return the result. It just creates a promise, and will log the testPromiseResolve() result later. I does not stop execution, it does not break anything. What you see there is exactly the async/await functionality: it can stop (and defer) the execution inside the async function body only.
– Bergi
Nov 21 at 14:53

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53407897%2freturned-value-from-ajax-bug-or-feature%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53407897%2freturned-value-from-ajax-bug-or-feature%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Berounka

Sphinx de Gizeh

Different font size/position of beamer's navigation symbols template's content depending on regular/plain...