See the ES2018 standard for full specification of the ECMAScript 2018 language.
ES2018 includes the following new feature proposals:
Async iterators are like iterators, but this time, next() returns a promise. This promise resolves with the tuple { value, done }. A promise needs to be returned because, at the time, the iterator returns the values of value and done are unknown.
function asyncIterator() {
const array = [1, 2];
return {
next: function() {
if (array.length) {
return Promise.resolve({
value: array.shift(),
done: false
});
} else {
return Promise.resolve({
done: true
});
}
}
};
}
var iterator = asyncIterator();
(async function() {
await iterator.next().then(console.log); // { value: 1, done: false }
await iterator.next().then(console.log); // { value: 2, done: false }
await iterator.next().then(console.log); // { done: true }
})();
Rest properties for object destructuring assignment.
let { fname, lname, ...rest } = { fname: "Hemanth", lname: "HM", location: "Earth", type: "Human" };
fname; //"Hemanth"
lname; //"HM"
rest; // {location: "Earth", type: "Human"}
Spread properties for object destructuring assignment.
let info = {fname, lname, ...rest};
info; // { fname: "Hemanth", lname: "HM", location: "Earth", type: "Human" }
Promise API is extended by an optional finally block which is called in any case (after the Promise is resolved or is rejected).
function testFinally() {
return new Promise((resolve,reject) => resolve())
}
testFinally().then(() => console.debug("resolved")).finally(() => console.debug("finally"))
// resolved
// finally