zoukankan      html  css  js  c++  java
  • 优雅地 `async/await`

    async/await 虽然取代了回调,使用类似同步的代码组织方式让代码更加简洁美观,但错误处理时需要加 try/catch

    比如下面这样,一个简单的 Node.js 中使用 async/await 的场景:

    const fetch = require("node-fetch");
    

    async function getData() {
    const url = "https://api.github.com/users/wayou";
    try {
    const response = await fetch(url);
    const data = await response.json();
    console.log(data);
    } catch (error) {
    throw error;
    }
    }

    getData();

    像这样的异步场景,Node.js 中会有很多。如果都通过 try/catch 来错误处理,数量多了之后也是不太美观的。

    将异步进行一层封装

    因为本质上 async/awaitPromise,我们可以封装一个简单的方法,将错误处理变得更优雅。

    比如下面这样:

    function await2js(promise) {
      return promise.then(result => [undefined, result]).catch(error => [error, undefined]);
    }

    该方法始终返回两个结果,第一个是错误,第二个是数据,这和 Node.js 中回调的入参 (err,data)=>void 是一致的,使得这层包装很 Node.js,一点也不会感到奇怪。

    所以改造后的使用示例:

    async function getData() {
      const url = "https://api.github.com/users/wayou";
      const [error, response] = await await2js(fetch(url));
      if (error) {
        throw error;
      }
    

    const [error2, data] = await await2js(response.json());
    if (error2) {
    throw error2;
    }

    console.log(data);
    }

    这层封装针对单个 await,不像 try/catch 那么粗犷一下子包含再次 await。也不是说 try/catch 不能精细地处理错误,但脑补一下把上面两次 await 都用 try/catch 的模样。

    当然,如果嫌麻烦,也可通过 Promise.all() 或 Promise 的链式调用将多次 await 操作合并,只处理一次错误。

    TypeScript 版本

    function await2js<T, K = Error>(promise: Promise<T>) {
      return promise
        .then<[undefined, T]>((response: T) => [undefined, response])
        .catch<[K, undefined]>((error: K) => [error, undefined]);
    }

    这里有个相应的 npm 包便是做这事情的 await-to-js

    相关资源

  • 相关阅读:
    Hibernate关于字段的属性设计
    Hibernate之增删查改常见错误
    Hibernate之实体类设计基本步骤
    Github开源之旅第二季-MarkDown
    8.Git命令(上)
    9.Git命令(下)
    7.Git Bash操作的四个坑(基本LINUX操作)
    Webserver-HTTP项目(深入理解HTTP协议)
    JAVA入门到精通-第94讲-山寨QQ项目8-好友在线提示
    JAVA入门到精通-第93讲-山寨QQ项目7-好友在线提示
  • 原文地址:https://www.cnblogs.com/Wayou/p/elegant_async_await.html
Copyright © 2011-2022 走看看