Junior — Middle
What was used for handling asynchronous operations before the appearance of Promise in JavaScript?
sobes.tech AI
Answer from AI
Before the appearance of Promise in JavaScript, asynchronous operations were mainly handled using callback functions (callback functions).
This meant that other code was passed into a function, which was supposed to execute after the asynchronous operation completed. This approach often led to what is called "callback hell" — nested and hard-to-read structures.
Example with callback:
function fetchData(callback) {
setTimeout(() => {
const data = "Data";
callback(data);
}, 1000);
}
fetchData(function(result) {
console.log(result);
});
Events (EventEmitter in Node.js) were also used, and sometimes libraries implementing their own mechanisms for managing asynchrony.