Promise, Async / await



Promise - это встроенный в JS механизм для асинхронного программирования



например:



const request = new Promise(resolve => {

setTimeout(() => {

resolve('response');

}, 2000);

});

request.then(response => {

console.log(`response is ${response}`);

});

console.log('will be printed first');



async / await упрощают работу с Promise:



function request() {

return new Promise(resolve => {

setTimeout(() => {

resolve('response');

}, 2000);

});

};



async function main() {

let response = await request();

console.log(`response is: ${response}`);

console.log('will be printed after response');

}



main();



Больше примеров и разъяснений вы найдете в статье



P.S. рекомендую после изучения Promise переходить к изучению RxJs - тк это более мощный и гибкий инструмент для асинхронного программирования