What is a Test Fixture ?
A test fixture is a fixed state of a set of objects used as a baseline for running tests. The purpose of test fixture is to ensure that there is a well known and fixed environment in which tests are run so that results are repeatable.
References
- https://github.com/junit-team/junit4/wiki/test-fixtures
Duck Typing in JavaScript ?
The term duck typing comes from the saying “If it looks like a duck, swims like a duck, and quacks like a duck, then it probably is a duck”.
Duck Typing helps to avoid conditional in JavaScript. Duck Typing helps to emulate interface interfaces in JavaScript.
References
- http://adripofjavascript.com/blog/drips/using-duck-typing-to-avoid-conditionals-in-javascript.html
- http://jscriptpatterns.blogspot.com/2013/01/javascript-interfaces.html
- https://en.wikipedia.org/wiki/Duck_typing
What is a Promise ?
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. A promise may be in one of the 3 possible states: fulfilled, rejected or pending. Promise user can attach callbacks to handle the fulfilled value or the reason for rejection.
then
The first argument of .then
is a function that runs when the promise is resolved and
receives the result. The second argument of .then
is a function that runs when
the promise is rejected and receives the error.
let promise = new Promise(function(resolve, reject) {
setTimeout(() => resolve("done!"), 1000);
});
// resolve runs the first function in .then
promise.then(
result => alert(result), // shows "done!" after 1 second
error => alert(error) // doesn't run
);
let promise = new Promise(function(resolve, reject) {
setTimeout(() => reject(new Error("Whoops!")), 1000);
});
// reject runs the second function in .then
promise.then(
result => alert(result), // doesn't run
error => alert(error) // shows "Error: Whoops!" after 1 second
);
Chaining
Value returned by then
is used in next then
by calling with that value.
However if a promise is returned, the next then
waits on it and is only
called when the promise settles.
catch
The catch
callback is executed when the promise is rejected.
catch
is just a syntactic sugar for then(undefined, func)
, but it’s more
readable.
let promise = new Promise(function(resolve, reject) {
reject("Error");
});
promise.then(function() {
console.log("success");
})
.catch(function() {
console.log("failure");
});
finally
finally
callback is called regardless of success or failure
Its a good handler for performing cleanup.
let promise = new Promise(function(resolve, reject) {
reject("Connection Timeout");
})
promise.then(function() {
console.log("success");
})
.catch(function() {
console.log("failure")
})
.finally(function() {
console.log("Cleaning up connection objects");
});
// failure
// Cleaning up connection objects
Static Methods
- Promise.resolve() - Creates a promise that resolves to value given to it.
- Promise.reject() - Create a promise that rejects with the value given to it.
- Promise.all(array) - Make a promise that fulfills when every itme in the array fulfills and rejects if any item rejects.
- Promise.race() - Make a promise that fulfills as soon as any item fulfills or rejects as soon as any item rejects.
Promise States
- Fulfilled - The action relating to the promise suceeded.
- Rejected - The action relating to the promise failed.
- Pending- Hasn’t fulfilled or rejected yet.
- Settled - Has fulfilled or rejected.
References
Big O notation
Big O notation is used in computer science to describe the the performance or complexity of an algorithm. Big O specifically describes the the worst case scenario, and can be used to describe the execution time required or the space used.
References
- https://rob-bell.net/2009/06/a-beginners-guide-to-big-o-notation/
- https://justin.abrah.ms/computer-science/big-o-notation-explained.html
- https://www.interviewcake.com/article/javascript/big-o-notation-time-and-space-complexity
JavaScript Closures
Closures are function which ‘remember’ the environment in which they were created. A closure is a combination of a function and the lexical environment within which the function was declared. This environment consists of any local variables that were in-scope at the time closure was created.
The word lexical refers to the fact that lexical scoping uses the location where a variable is declared within the source code to determine where that variable is available. Nested functions have access to variables declared in their outer scope.
function foo() {
var i = 10;
function print() {
console.log(i);
}
return print;
}
var printFunc = foo();
printFunc(); // 10
Common mistake of using closure in for loops
for (var x = 0; x < 10; x++) {
setTimeout(function() {
console.log(x);
}, 10);
}
// prints 10, 10 times
Solution, is to use a function factory
function myFunc(p) {
return function() {
console.log(p);
}
}
for (var x = 0; x < 10; x++) {
setTimeout(myFunc(x), 10);
}
// prints 0, 1, 2, 3, 4, 5, 6, 7, 8, 9