Async and performance Flashcards

1
Q

explain JS engine and the hosting environment

A

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

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

explain event loop

A

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.

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

difference between async and parallel

A

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

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

explain concurrency

A

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.

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

What is Thenable object?

A

Any obect that has which has a then(..) method on it. It is assumed that any such value is a Promise-conforming thenable.

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

Are promises called asynchronously?

A

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

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

What does Promise.resolve() do?

A

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.

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

When should Promise.resolve be called?

A

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.

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

what happens when a reject handler is not supplied for. Promise handler?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
what is the output?
function foo() {
	setTimeout( function(){
		baz.bar();
	}, 100 );
}

try {
foo();

}
catch (err) {
console.log(“Exception occured”)
}

A

The error never gets caught because try/catch in sync and the exception happens in the setTimeout which is async

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q
what is the output?
var p = Promise.reject( "Oops" );
p.then(
	function fulfilled(){
	},
	function rejected(err){
		console.log( err ); 
	}
);
A

Oops

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

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")
	}
);
A

Exception will be thrown.

The rejected handler won’t get called since, the error handler is for the promise p

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

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 );
A

It will be thrown as a global error in

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

What does Promise.all([ .. ]) do?

A

Takes in an array of promises, executes them on parallel and calls the success/reject handlers appropriately

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

what does Promise.race([ .. ]) do?

A

Takes in an array of promises and resolves as soon as the first one completes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q
what is the output?
var p1 = Promise.resolve( 42 );
var p2 = Promise.resolve( "Hello World" );
var p3 = Promise.reject( "Oops" );

Promise.race( [p1,p2,p3] )
.then( function(msg){
console.log( msg );
} );

Promise.all( [p1,p2,p3] )
.catch( function(err){
console.error( err );
} );

Promise.all( [p1,p2] )
.then( function(msgs){
console.log( msgs );
} );

A

42
“Oops”
[42,”Hello World”]

17
Q

var p = foo( 42 )
.then( STEP2 )
.then( STEP3 );

what happens when an error gets thrown in one of the steps

A

since none of the promises have an error handler it will propagate indefinitely.

p.catch(…)
will handle the error

18
Q

How to pause a function execution at some point and resume later

A

Use of “yield” keyword

19
Q
what is the output?
function *foo() {
	var x = yield 2;
	z++;
	var y = yield (x * z);
	console.log( x, y, z );
}

var z = 1;

var it1 = foo();
var it2 = foo();
var val1 = it1.next().value;			
var val2 = it2.next().value;			
val1 = it1.next( val2 * 10 ).value;		
val2 = it2.next( val1 * 5 ).value;		

it1. next( val2 / 2 );
it2. next( val1 / 4 );

A

20, 300, 3

200 10 3

20
Q

How to make an object iterable ?

A

return {

	[Symbol.iterator]: function(){ return this; }, }
21
Q

How to get all the properties of an Object?

A
var obj = new SomeObject()
var props = Object.keys(obj)
//This method doesnt return the properties from teh prototype