You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+66Lines changed: 66 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -321,6 +321,72 @@ if (result) {
321
321
**Prompt**
322
322
Displays a message to the user and also takes input from the user. It returns the input value to the user.
323
323
324
+
## Callbacks
325
+
326
+
Callbacks are JavaScript's way of implementing asynchronous programming. A callback is a function that is passed as an argument to another function and is executed after its parent function has been completed.
327
+
328
+
javascript
329
+
function callback() {
330
+
console.log("Callback function");
331
+
}
332
+
333
+
function parentFunction(callback) {
334
+
console.log("Parent function");
335
+
callback();
336
+
}
337
+
338
+
339
+
If the callback is an anonymous function, then we can also directly call the function at the time of parent call.
340
+
341
+
javascript
342
+
function parentFunction(callback) {
343
+
console.log("Parent function");
344
+
callback();
345
+
}
346
+
347
+
parentFunction(function() {
348
+
console.log("Callback function");
349
+
});
350
+
351
+
352
+
## Arrow functions
353
+
354
+
These are a type of functions that are used to write the functions in a short way. They are anonymous functions, with a special syntax.
355
+
356
+
It is named an arrow function, because of the arrow symbol `=>` in the syntax.
357
+
358
+
javascript
359
+
var functionName = (param1, param2, ...) => {
360
+
// function body
361
+
return value;
362
+
}
363
+
364
+
365
+
If the function has only one statement, then we can remove the curly braces and the return keyword.
366
+
367
+
javascript
368
+
var functionName = (param1, param2, ...) => statement;
369
+
370
+
371
+
If the function has only one parameter, then we can remove the parenthesis.
372
+
373
+
javascript
374
+
var functionName = param1 => {
375
+
// function body
376
+
return value;
377
+
}
378
+
379
+
380
+
They are generally used with JS's native functions like `map`, `filter`, `reduce`, etc. When we need to apply the same operation to a set of values.
381
+
382
+
## Promises
383
+
384
+
These are the basics of asynchronous programming in JS. A promise is an object that may produce a single value some time in the future. Either a resolved value or a reason that it's not resolved.
0 commit comments