Jun 01, 2021 Article blog
This article was reproduced from the public number: Code Secret Garden
JavaScript Engine V8 is released in version 8.5, and let's take a look at the three practical features of version 8.5.
Promise
adds an
any
method that receives a
Promise
array, and when one of the
Promise
in the array is
fulfilled
the
Promise
it returns is returned.
const promises = [
fetch('/endpoint-a').then(() => 'a'),
fetch('/endpoint-b').then(() => 'b'),
fetch('/endpoint-c').then(() => 'c'),
];
try {
const first = await Promise.any(promises);
// 任何一个 Promise 为 fulfilled 状态
console.log(first);
// → 'b'
} catch (error) {
// 所有 Promise 都被 rejected 了
console.assert(error instanceof AggregateError);
// reject 结果数组
console.log(error.errors);
}
If all
Promise
of Promise are rejected,
Promise.any
returns an exception of
AggregateError
type, which
errors
all
Promise
rejected properties.
Be careful not to confuse with
Promise.race
race
which returns if any one of
Promise
in the array is resolved or rejected, while
any
method must have a resolution that throws an exception if all are rejected.
String.prototype.replaceAll
provides an easy way to replace all matches for substrings without creating a global
RegExp
Looking at the following example, before you're going to replace all of the
+
in
queryString
you need to create a global rule:
const queryString = 'q=query+string+parameters';
queryString.replace(/\+/g, ' ');
Now you just need to use
replaceAll
method:
queryString.replaceAll('+', ' ');
The logical assignment operator is a new compound assignment operator that allows him to manipulate logic
&&,||或??
Combine with an assignment into a command.
x &&= y;
// 等同于 x && (x = y)
x ||= y;
// 等同于 x || (x = y)
x ??= y;
// 等同于 x ?? (x = y)
Here's a look at
W3Cschool编程狮
introduction to
the three practical new features that JavaScript engine V8 8.5 brings.