The for-await-of syntax is similar to the for-of iteration. The key difference is that it automatically awaits any promises generated by the iterator. This lesson covers how to consume async data sources easily with for-await-of.

for-await-of essentially allows you to use async/await in a generator function.

import { magic } from './server';

(Symbol as any).asyncIterator =
(Symbol as any).asyncIterator
|| Symbol.for("Symbol.asyncIterator"); async function* numbers() {
let index = ;
while (true) {
yield index;
index = await magic(index);
if (index > ) {
break;
}
}
} async function main() {
for await (const num of numbers()) {
console.log(num);
}
}
main();

Server:

const magic = (i: number) => new Promise<number>(res => setTimeout(res(i + ), ));

Example 2:

// Asynchronous generators

async function asyncGenerators () {
async function* createAsyncIterable(syncIterable) {
for (const elem of syncIterable) {
yield elem;
}
} const asyncGenObj = createAsyncIterable(['a', 'b']);
const [{value:v1}, {value:v2}] = await Promise.all([
asyncGenObj.next(), asyncGenObj.next()
]);
console.log("v1", v1, "v2", v2); // a, b
}
asyncGenerators();

[TypeScript] Asynchronous Iteration using for-await-of的更多相关文章

  1. The Task: Events, Asynchronous Calls, Async and Await

    The Task: Events, Asynchronous Calls, Async and Await Almost any software application today will lik ...

  2. 【TypeScript】如何在TypeScript中使用async/await,让你的代码更像C#。

    [TypeScript]如何在TypeScript中使用async/await,让你的代码更像C#. async/await 提到这个东西,大家应该都很熟悉.最出名的可能就是C#中的,但也有其它语言也 ...

  3. [微信小程序] 终于可以愉快的使用 async/await 啦

    [小程序] 终于可以愉快的使用 async/await 啦 这篇文章主要是想说一下 怎么在微信小程序中使用async/await从而逃离回调地狱 背景 最近一直在搞微信小程序 用的语言是TypeScr ...

  4. angular2 学习笔记 ( Rxjs, Promise, Async/Await 的区别 )

    Promise 是 ES 6 Async/Await 是 ES 7 Rxjs 是一个 js 库 在使用 angular 时,你会经常看见这 3 个东西. 它们都和异步编程有关,有些情况下你会觉得用它们 ...

  5. ABP .Net Core 调用异步方法抛异常A second operation started on this context before a previous asynchronous operation completed

    1.  问题描述 最近使用ABP .Net Core框架做一个微信开发,同时采用了一个微信开发框架集成到ABP,在微信用户关注的推送事件里调用了一个async 方法,由于没有返回值,也没做任何处理,本 ...

  6. FOUNDATION OF ASYNCHRONOUS PROGRAMMING

    The async and await keywords are just a compiler feature. The compiler creates code by using the Tas ...

  7. 由LazyMan联想到的

    LazyMan问题与解法 http://mp.weixin.qq.com/s/drNGvLZddQztcUzSh8OsSw 给出了一道题目,并给出了解法: 题目: 实现一个LazyMan,可以按照以下 ...

  8. 10分钟学会ES7+ES8

    撰文为何 身为一个前端开发者,ECMAScript(以下简称ES)早已广泛应用在我们的工作当中.了解ECMA机构流程的人应该知道,标准委员会会在每年的6月份正式发布一次规范的修订,而这次的发布也将作为 ...

  9. es7,es8

    ES7新特性 ES7在ES6的基础上添加了三项内容:求幂运算符(**).Array.prototype.includes()方法.函数作用域中严格模式的变更. Array.prototype.incl ...

随机推荐

  1. Golang 源码剖析:log 标准库

    Golang 源码剖析:log 标准库 原文地址:Golang 源码剖析:log 标准库 日志 输出 2018/09/28 20:03:08 EDDYCJY Blog... 构成 [日期]<空格 ...

  2. Qt编程—去掉标题栏和设置窗口透明用法

    学习Qt编程,有时候我们很想做出好看又比较炫的画面,这时就常用到qt上的一些技巧. 这里我以一个小例子来展示qt的这些技巧,此qt编程写的,如图:(去掉标题栏和设置窗口透明后) 代码实现部分: .h文 ...

  3. php $_SERVER['PHP_SELF']安全漏洞

    REQUEST_URI 返回的是包括后面数据串的地址,如 index.php?str=1234 PHP_SELF 是 index.php ------------------------------- ...

  4. android:px,dp(dip),sp的差别

    1.px:表示屏幕的实际像素,比如320*480的屏幕在横向有320个像素,在纵向有480个像素,假设指定的某个空间的单位为px.那么在不同分辨率下的手机上.显示的都是指定的大小.一般不推荐使用px. ...

  5. 【c++版数据结构】之顺序表的实现

    SeqList.h #ifndef SEQLIST_H #define SEQLIST_H #include<iostream> using namespace std; typedef ...

  6. RandomAccessFile操作文件

    package file; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; ...

  7. ThinkPHP5.0框架开发--第8章 TP5.0 模型

    ThinkPHP5.0框架开发--第8章 TP5.0 模型 第8章 TP5.0 模型 ================================================= 今日学习 1. ...

  8. Python 异常(Exception)

    1. 字符串为构造函数的参数 >> raise Exception('hyperdirve overload') Exception Traceback (most recent call ...

  9. vue-cli全引入jquery

    欢迎加入前端交流群交流知识&&获取视频资料:749539640 vue-cli全引入jquery:(vue-cli使用webpack) 第一步: 在package.json文件里的de ...

  10. Web开发中 前端路由 实现的几种方式和适用场景

    http://blog.csdn.net/xllily_11/article/details/51820909