Asynchronous JS Flashcards Preview

Phase_2 > Asynchronous JS > Flashcards

Flashcards in Asynchronous JS Deck (5)
Loading flashcards...

How would you explain the use of callbacks in JS?

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

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


Explain how you would manage asynchronous flow control.

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 would you use setTimeout to sleep in JS?

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.



Creating a callback!

var fun = function(callback){ callback && callback( ) };
var yolo = function( ){ console.log('lola') };
fun( ) // undefined
fun(yolo) // lola


How to 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.