I know this is probably simple, but I can't figure it out with plain javascript. I am doing a fetch call and setting a variable right after it, which I know is not possible without a callback or ES7 await, etc. The problem is I have to use ES6 or earlier. How do I set a variable on the result right after I run the fetch call?
postData(baseUrl)
.then(data =>
Qualtrics.SurveyEngine.setEmbeddedData("QuestionsForValidation", (data))) // JSON-string from `response.json()` call
.catch(error => console.error(error));
function postData(url) {
// Default options are marked with *
return fetch(url, {
method: "GET", // *GET, POST, PUT, DELETE, etc.
mode: "cors",
// include, *same-origin, omit
headers: {
"Content-Type": "application/json",
"x-api-token": Qualtrics.SurveyEngine.getEmbeddedData("APIToken"),
"accept": "application/json"
}
})
.then(response => response.json()); // parses response to JSON
}
var JSONString = JSON.stringify(Qualtrics.SurveyEngine.getEmbeddedData("QuestionsForValidation").result)
It is not setting the variable because javascript is asynchronous. I want the variable to be set from the result of the fetch call. Please let me know if I need to clarify anything.