Answer the question
In order to leave comments, you need to log in
Why doesn't async/await work?
I use typescript ("lib": ["dom", "es2015.promise", "es5"])
Example:
function exec(url) {
return new Promise(resolve => {
let responce = null;
$.ajax({
type: 'GET',
url: url
}).done((data) => {
resolve(data);
});
});
}
async function request(url) {
return await exec(url);
}
let data = request('/ru/catalog');
console.log(data);
: "pending"
: "["undefined"]"
Answer the question
In order to leave comments, you need to log in
In order not to grow callback hell, use a construction inside which there will be all async code.
async function request(url) {(
try {
await function(){
$.ajax({
type: 'GET',
url: url,
success: (data) =>{
return data;
}
})
};
} catch (error) {
console.error(error);
throw error;
}
}
(async ()=>{
try {
await ....
// весь асинк код
let data = await request('/ru/catalog');
console.log(data);
return 1;
} catch (error) {
console.error(error);
throw error;
}
})();
let data = await request('/ru/catalog');
let data = request('/ru/catalog').then((data) => console.log(data));
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question