Performance: Promises for cleaner async
MEDIUM IMPACT
This concept affects how asynchronous operations impact the event loop and responsiveness of the application.
function fetchData() {
return new Promise((resolve, reject) => {
asyncHttpRequest('https://api.example.com/data', (err, data) => {
if (err) reject(err);
else resolve(data);
});
});
}
fetchData().then(result => console.log(result));function fetchData() {
const data = syncHttpRequest('https://api.example.com/data');
return data;
}
const result = fetchData();
console.log(result);| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Synchronous blocking call | N/A | Blocks event loop | Blocks rendering | [X] Bad |
| Async with Promises | N/A | Non-blocking | No blocking, smooth rendering | [OK] Good |