Async and performance Flashcards
explain JS engine and the hosting environment
JS engine simply runs the code it is requested to by the hosting environment. The hosting environment could mostly be Browser or server environment like the node js
explain event loop
It is basically similar to the event loop mechanism in window, where a loop continuously waits for some event to happen so that it can be processed.
This whole thing happens in the Engine.
difference between async and parallel
Async is the difference between now and Later.Example ajax call, the point where the call is made is now and the callback which executes when the response comes back is later.
Parallel is about ability of multiple things running simultaneously
explain concurrency
Concurrency is when two or more “processes” are executing simultaneously over the same period, regardless of whether their individual constituent operations happen in parallel (at the same instant on separate processors or cores) or not.
What is Thenable object?
Any obect that has which has a then(..) method on it. It is assumed that any such value is a Promise-conforming thenable.
Are promises called asynchronously?
That is, when you call then(..) on a Promise, even if that Promise was already resolved, the callback you provide to then(..) will always be called asynchronously
What does Promise.resolve() do?
Promise.resolve(..) will accept any thenable, and will unwrap it to its non-thenable value. But you get back from Promise.resolve(..) a real, genuine Promise in its place, one that you can trust. If what you passed in is already a genuine Promise, you just get it right back, so there’s no downside at all to filtering through Promise.resolve(..) to gain trust.
When should Promise.resolve be called?
It should be called when we call a method, returning promise or a “then able” object but we don’t trust that its return value to be well behaving promise.
what happens when a reject handler is not supplied for. Promise handler?
an assumed rejection handler is substituted:the assumed rejection handler simply rethrows the error, which ends up forcing the chained promise to reject with the same error reason. In essence, this allows the error to continue propagating along a Promise chain until an explicitly defined rejection handler is encountered.
what is the output? function foo() { setTimeout( function(){ baz.bar(); }, 100 ); }
try {
foo();
}
catch (err) {
console.log(“Exception occured”)
}
The error never gets caught because try/catch in sync and the exception happens in the setTimeout which is async
what is the output? var p = Promise.reject( "Oops" );
p.then( function fulfilled(){ }, function rejected(err){ console.log( err ); } );
Oops
what is the output?
var p = Promise.resolve( 42 );
p.then( function fulfilled(msg){
console.log( msg.toLowerCase() ); }, function rejected(err){ console.log("error occured") } );
Exception will be thrown.
The rejected handler won’t get called since, the error handler is for the promise p
What happens when an error is thrown inside the done handler?
var p = Promise.resolve( 42 );
p.then( function fulfilled(msg){ // numbers don't have string functions, // so will throw an error console.log( msg.toLowerCase() ); } ) .done( null, handleErrors );
It will be thrown as a global error in
What does Promise.all([ .. ]) do?
Takes in an array of promises, executes them on parallel and calls the success/reject handlers appropriately
what does Promise.race([ .. ]) do?
Takes in an array of promises and resolves as soon as the first one completes