From 01b0e12ba27b5069248fd09235e9a7143915ee30 Mon Sep 17 00:00:00 2001 From: Irvin Date: Wed, 16 Feb 2022 02:02:49 +0800 Subject: remove `notranslate` class in zh-CN --- .../reference/statements/async_function/index.html | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) (limited to 'files/zh-cn/web/javascript/reference/statements/async_function') diff --git a/files/zh-cn/web/javascript/reference/statements/async_function/index.html b/files/zh-cn/web/javascript/reference/statements/async_function/index.html index 37aad08974..b4bdd88173 100644 --- a/files/zh-cn/web/javascript/reference/statements/async_function/index.html +++ b/files/zh-cn/web/javascript/reference/statements/async_function/index.html @@ -23,7 +23,7 @@ translation_of: Web/JavaScript/Reference/Statements/async_function

语法

-
async function name([param[, param[, ... param]]]) {
+
async function name([param[, param[, ... param]]]) {
     statements 
 }
 
@@ -65,14 +65,14 @@ translation_of: Web/JavaScript/Reference/Statements/async_function

例如,如下代码:

-
async function foo() {
+
async function foo() {
    return 1
 }
 

等价于:

-
function foo() {
+
function foo() {
    return Promise.resolve(1)
 }
 
@@ -81,14 +81,14 @@ translation_of: Web/JavaScript/Reference/Statements/async_function

例如:

-
async function foo() {
+
async function foo() {
    await 1
 }
 

等价于

-
function foo() {
+
function foo() {
    return Promise.resolve(1).then(() => undefined)
 }
 
@@ -103,7 +103,7 @@ translation_of: Web/JavaScript/Reference/Statements/async_function
  • 一段时间后,同样当第二个promise完结的时候,result2将被赋值为2,之后函数将会正常同步执行,将默认返回undefined 。
  • -
    async function foo() {
    +
    async function foo() {
        const result1 = await new Promise((resolve) => setTimeout(() => resolve('1')))
        const result2 = await new Promise((resolve) => setTimeout(() => resolve('2')))
     }
    @@ -113,7 +113,7 @@ foo()

    例如,在下面的代码中,在promise链上配置了.catch处理程序,将抛出未处理的promise错误。这是因为p2返回的结果不会被await处理。

    -
    async function foo() {
    +
    async function foo() {
        const p1 = new Promise((resolve) => setTimeout(() => resolve('1'), 1000))
        const p2 = new Promise((_,reject) => setTimeout(() => reject('2'), 500))
        const results = [await p1, await p2] // 不推荐使用这种方式,请使用 Promise.all或者Promise.allSettled 
    @@ -126,7 +126,7 @@ foo().catch(() => {}) // 捕捉所有的错误...

    简单例子

    -
    var resolveAfter2Seconds = function() {
    +
    var resolveAfter2Seconds = function() {
       console.log("starting slow promise");
       return new Promise(resolve => {
         setTimeout(function() {
    @@ -238,7 +238,7 @@ setTimeout(parallelPromise, 13000); // same as parallel
     
     

    返回 {{jsxref("Promise")}}的 API 将会产生一个 promise 链,它将函数肢解成许多部分。例如下面的代码:

    -
    function getProcessedData(url) {
    +
    function getProcessedData(url) {
       return downloadData(url) // 返回一个 promise 对象
         .catch(e => {
           return downloadFallbackData(url)  // 返回一个 promise 对象
    @@ -250,7 +250,7 @@ setTimeout(parallelPromise, 13000); // same as parallel
     
     

    可以重写为单个async函数:

    -
    async function getProcessedData(url) {
    +
    async function getProcessedData(url) {
       let v;
       try {
         v = await downloadData(url);
    @@ -270,7 +270,7 @@ setTimeout(parallelPromise, 13000); // same as parallel
     
     

    看下下面重写的上面代码,在processDataInWorker抛出异常时返回了null:

    -
    async function getProcessedData(url) {
    +
    async function getProcessedData(url) {
       let v;
       try {
         v = await downloadData(url);
    -- 
    cgit v1.2.3-54-g00ecf