x с помощью функции x_value, но в консоли выходить undefined, т.е. внутри функции сначала срабатывает return a;, а потом уже $.getJSON. Как грамотно это сделать?function x_value() {
var a;
$.getJSON("./json.json", function (data) {
var b = data["key_1"];
a = b.toString();
});
return a;
}
var x = x_value(); let x;
function abc(num){
return () => {
return num**2;
}
}
x = abc(4);
console.log(x()); // 16
(async ()=>{
let x = await promisify($.getJSON)('./json.json');
console.log( x ) // ...твоя data...
})();
// A simplified implementation of `util.promisify()`. Doesn't
// cover all cases, don't use this in prod!
function promisify(fn) {
return function() {
const args = Array.prototype.slice.call(arguments);
return new Promise((resolve, reject) => {
fn.apply(this, [].concat(args).concat([(err, res) => {
if (err != null) {
return reject(err);
}
resolve(res);
}]));
});
};
}