Asynchronous JS Flashcards

1
Q

How would you explain the use of callbacks in JS?

A

callbacks are first class functions which get called by other functions.

function mySandwich(param1, param2, callback) {
alert(‘Fave sandwiches: ‘ + param1 + ‘ and ‘ + param2);
callback( );
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Explain how you would manage asynchronous flow control.

A

Asynchronous programming is the process of writing programs which allow other programs to start doing their work before former programs finish executing. Asynchronous flow control describes the order by which javaScript functions are run. Asynchronous flow control also describes the order by which statements of an imperative program, programs which utilize functional statements that change a programs state, are run.

There are THREE APPROACHES to writing async programming: nested callback functions, promises and the async.js library.

Nested callbacks are the intuitive way of handling asynchronicity, we’re simply executing functions inside other functions, only after the original function was successfully called and thereby we have asynchronicity. Beware that callback hell is highly likely.

Promises give you a better handle on your async calls by giving more formalized structure to them and helping you avoid callback hell. We’re mostly stringing functions together using then( ).

The async.js library is great.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How would you use setTimeout to sleep in JS?

A

setTimeout calls a function or executes a code snippet after a specified delay.

var timeoutID = scope.setTimeout( first_class_function, delay );

var timeoutID = window.setTimeout( first_class_function, delay );

// or don’t include ‘window’ and just run setTimeout( ) inside your functions scope.

window.clearTimeout(timeoutID);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Creating a callback!

A
var fun = function(callback){ callback && callback( ) };
var yolo = function( ){ console.log('lola') };
fun( )           // undefined
fun(yolo)    // lola
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How to use git pull - -rebase?

A

https://stackoverflow.com/questions/2472254/when-should-i-use-git-pull-rebase

Running this command merges your remote work or your teams remote work on the remote branch with the work you have on your local branch. After doing this you can then push your work as well as the pulled content. This is really helpful in avoiding any merge conflicts.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly