【AICon】探索RAG 技术在实际应用中遇到的挑战及应对策略!AICon精华内容已上线73%>>> 了解详情
写点什么

Javascript 之 async 四重奏(三)

  • 2019-12-31
  • 本文字数:1072 字

    阅读完需:约 4 分钟

Javascript之async四重奏(三)

async-await 的兼容并包

async 函数

在 es7 中引入的 async 函数使得处理异步操作更加方便:


直接在普通函数前面加上 async,表示这是一个异步函数,在要异步执行的语句前面加上 await,表示后面的表达式需要等待。我们通过下面的例子先简单了解一下:


(注:左右/上下滑动屏幕可查看全部代码)


function timeout(ms) { return new Promise((resolve) => {   setTimeout(resolve, ms); });}async function asyncPrint(value, ms) { await timeout(ms); console.log(value);}asyncPrint('hello world', 50);
复制代码


上面的代码指定 50 毫秒后输出 hello world


async 函数有多种使用方式:


// 函数声明式:


async function foo() {}


// 函数表达式:


const foo = async function () {}


// 对象的方法:


let obj = {async foo() {}}


obj.foo().then()


// 箭头函数:


const foo = async () => {}


async 函数返回一个 promise 对象,可以使用 then 方法添加回调函数:


(注:左右/上下滑动屏幕可查看全部代码)


async function f() {return 'hello world';}f().then(v => console.log(v))// "hello world"
复制代码


Await 命令后面是一个 promise 对象,如果不是会被转成一个立即 resolve 的 promise 对象:


(注:左右/上下滑动屏幕可查看全部代码)


async function f() {return await 123;}f().then(v => console.log(v))// 123
复制代码


通过 async 函数来发送多个请求:


(注:左右/上下滑动屏幕可查看全部代码)


async function dbFuc(db) {let docs = ['/posts.json', '/gets.json', '/puts.json'];let promises = docs.map((doc) => db.post(doc));let results = await Promise.all(promises);console.log(results);}
复制代码


由此可见,async 函数不仅包含了 promise、generator,代码也更加简洁。


总的来说,async 函数就是 generator 函数的语法糖,相对于 generator 函数 async 函数有以下优点:


  1. 有内置执行器,不用调用 next

  2. Generator 函数是需要调用 next 指令来运行异步的语句,async 不需要调用 next,直接像运行正常的函数那样运行就可以

  3. 有更好的语义化

  4. 语义化更明确,相比较于 Generator 的*和 yield,async 和 await 更明确。

  5. await 后面可以跟 promise 或者任意类型的值

  6. yield 命令后面只能是 Thunk 函数或 Promise 对象,而 async 函数的 await 命令后面,可以是 Promise 对象和原始类型的值(数值、字符串和布尔值,但这时等同于同步操作)。

  7. 返回一个 promise 对象,可以调用.then

  8. async 直接返回一个 promise 对象,可以用 then 和 catch 来处理。


本文转载自 Think 体验设计公众号。


原文链接:https://mp.weixin.qq.com/s/QgC2OjVF4pvltte9V_gkIQ


2019-12-31 15:42508

评论

发布
暂无评论
发现更多内容

Android Switch控件修改样式

android 程序员 移动开发

Nebula 分布式图数据库介绍

Se7en

Android 三类框架的理解以及MVVM框架的使用

android 程序员 移动开发

Android 使用讯飞语音SDK

android 程序员 移动开发

android 三级级联筛选列表

android 程序员 移动开发

Android 中高级核心复习面试题整理,备战年后金三银四!

android 程序员 移动开发

【译】TypeScript的Record类型说明

废材壶

typescript

Android Studio 插件

android 程序员 移动开发

Android Studio安装更新终极解决方式

android 程序员 移动开发

Android TextView的属性与应用

android 程序员 移动开发

Android WebView常见问题

android 程序员 移动开发

Android C++系列:JNI基本操作

轻口味

android 11月日更

Android SDK 网络模块解析

android 程序员 移动开发

Android Studio 教程:入门开发第一个程序

android 程序员 移动开发

Android _ 从 Dagger2 到 Hilt 玩转依赖注入(一)

android 程序员 移动开发

android 中DrawerLayout实现抽屉

android 程序员 移动开发

Android OpenCV(三十七):轮廓外接多边形

android 程序员 移动开发

[ CloudWeGo 微服务实践 - 06 ] 服务发现(1)

baiyutang

golang 微服务 11月日更

Android SDK 网络模块解析(1)

android 程序员 移动开发

Android Studio 4

android 程序员 移动开发

Android UI--ViewPager扩展Tab标签指示

android 程序员 移动开发

Android 主流通用常用框架汇总(持续更新)

android 程序员 移动开发

Android P 适配指南

android 程序员 移动开发

Android VideoPlayer

android 程序员 移动开发

Android Zygote 从何而来?揭开Android系统启动的面纱

android 程序员 移动开发

android 五大应用开发框架

android 程序员 移动开发

Android 原生控件ViewFlipper实现淘宝头条垂直滚动广告条

android 程序员 移动开发

android okhttp异步请求使用详解 (2)

android 程序员 移动开发

在Electron中简单实现拖拽功能

废材壶

node.js 大前端 Electron

Android SpannableString详细解析

android 程序员 移动开发

Android View 绘制流程

android 程序员 移动开发

Javascript之async四重奏(三)_语言 & 开发_Think体验设计_InfoQ精选文章