wiki/content/20200911150451-callbacks.md

28 lines
653 B
Markdown
Raw Normal View History

2024-05-06 20:40:05 +00:00
---
2024-10-30 17:04:36 +00:00
date: 20200911
2024-05-06 20:40:05 +00:00
id: 6b091d50-aa10-4ba8-a652-43cd5433371e
title: JavaScript Callbacks
---
# Examples
## setTimeout
``` javascript
setTimeout(() => { console.log("This will echo after 2 seconds!"); }, 2000);
```
# ES6
## Best practices
### Prefer arrow functions as callbacks
As callbacks, [arrow functions](20201006111349-arrow_functions) have two
advantages over traditional functions:
- `this` is lexical and therefore safer to use.
- Their syntax is more compact. That matters especially in functional
programming, where there are many higher-order functions and methods
(functions and methods whose parameters are functions).