ES6 modules are implicitly in strict mode.
See the spec
10.2.1 Strict Mode Code
* Module code is always strict mode code.
In ES6 functions can have default parameters:
function add(a, b=1) {
return a + b;
}
add(1); // 2
add(1, 2); // 3Beware that default parameters are:
- evaluated at call time (a new object is created each time the function is called, unlike Python)
- default objects do not "traverse" properties, the entire object is "defaulted" (see example)
// accept 'first' and 'second' options
function f(options={ second=42 }) {
options.first //...
options.second //...
}
f(); // first=undefined, second=42
f({first: 41}); // first=41, second=undefinedSee MDN
If an error is thrown during the promise constructor or in one of the .then
callbacks (onFulfilled/onRejected), the error is automatically caught and the
promise rejected.
This is a good thing, however it can be perturbing when debugging. If no onRejected callback is attached the error will be "swallowed" and the failure silent.
For example:
const promise = new Promise(function() {
throw new Error('boom');
});
// 'promise' is rejected, no 'onRejected' callback is attached so the failure is silentSimilarly:
const promise = new Promise(function(resolve, reject) {
resolve(42);
});
const promise2 = promise.then(function(value) {
throw new Error('boom');
});
// 'promise2' is now rejected, no onRejected callback so the failure is silentThe RSVP library helps solving this problem by proposing an error handler
RSVP.on('error', function(error) { // ... });See JavaScript Promises - html5rocks
Rejections happen [...] implicitly if an error is thrown in the constructor callback.
[...] errors are automatically caught and become rejections.
The same goes for errors thrown in "then" callbacks.
See section 7. ii - Promises/A+ spec
If either onFulfilled or onRejected throws an exception e, promise2 must be rejected with e as the reason.
See also an interesting discussion on the v8 repository about this issue.