Answer the question
In order to leave comments, you need to log in
How to get return value from ajax request?
Good afternoon!
I registered my js-library for working with goods in the Bitrix24 box.
I ran into a problem:
I make a request to the module through BX.ajax.runAction, for convenience I wrapped the call in a separate asynchronous function:
ProductData.post = async function (actionName, params = {}) {
let action = 'ramapriya:productdata.api.calculator.' + actionName;
const request = await BX.ajax.runAction(action, {
method: 'POST',
data: params
});
return await request.data;
}
const inputNames = this.post('getProductPropertiesNames', {
productId: this.extractProductId(window.location.href)
});
let inputs;
inputNames.then(response => {
console.log(response.inputs) // работает, возвращает нужный результат
inputs = response.inputs;
})
console.log(inputs) // undefined
Answer the question
In order to leave comments, you need to log in
The word async has one simple meaning: this function always returns a promise. Values of other types are wrapped in a successfully completed promise automatically - Source . To get data, you need to use an asynchronous function or through then.
Example 1
...
ProductData.post().then(data => console.log(data));
(async function() {
const response = await ProductData.post();
console.log(response);
})()
inputNames.then(response => {
console.log(response.inputs) // работает, возвращает нужный результат
test(response.inputs);
})
function test(inputs) {
console.log(inputs)
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question