[TypeScript] Asynchronous Iteration using for-await-of
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的更多相关文章
- The Task: Events, Asynchronous Calls, Async and Await
The Task: Events, Asynchronous Calls, Async and Await Almost any software application today will lik ...
- 【TypeScript】如何在TypeScript中使用async/await,让你的代码更像C#。
[TypeScript]如何在TypeScript中使用async/await,让你的代码更像C#. async/await 提到这个东西,大家应该都很熟悉.最出名的可能就是C#中的,但也有其它语言也 ...
- [微信小程序] 终于可以愉快的使用 async/await 啦
[小程序] 终于可以愉快的使用 async/await 啦 这篇文章主要是想说一下 怎么在微信小程序中使用async/await从而逃离回调地狱 背景 最近一直在搞微信小程序 用的语言是TypeScr ...
- angular2 学习笔记 ( Rxjs, Promise, Async/Await 的区别 )
Promise 是 ES 6 Async/Await 是 ES 7 Rxjs 是一个 js 库 在使用 angular 时,你会经常看见这 3 个东西. 它们都和异步编程有关,有些情况下你会觉得用它们 ...
- ABP .Net Core 调用异步方法抛异常A second operation started on this context before a previous asynchronous operation completed
1. 问题描述 最近使用ABP .Net Core框架做一个微信开发,同时采用了一个微信开发框架集成到ABP,在微信用户关注的推送事件里调用了一个async 方法,由于没有返回值,也没做任何处理,本 ...
- FOUNDATION OF ASYNCHRONOUS PROGRAMMING
The async and await keywords are just a compiler feature. The compiler creates code by using the Tas ...
- 由LazyMan联想到的
LazyMan问题与解法 http://mp.weixin.qq.com/s/drNGvLZddQztcUzSh8OsSw 给出了一道题目,并给出了解法: 题目: 实现一个LazyMan,可以按照以下 ...
- 10分钟学会ES7+ES8
撰文为何 身为一个前端开发者,ECMAScript(以下简称ES)早已广泛应用在我们的工作当中.了解ECMA机构流程的人应该知道,标准委员会会在每年的6月份正式发布一次规范的修订,而这次的发布也将作为 ...
- es7,es8
ES7新特性 ES7在ES6的基础上添加了三项内容:求幂运算符(**).Array.prototype.includes()方法.函数作用域中严格模式的变更. Array.prototype.incl ...
随机推荐
- docker mysql 数据持久化到本地、设置不区别表名大小写-清风柳絮-51CTO博客
原文:docker mysql 数据持久化到本地.设置不区别表名大小写-清风柳絮-51CTO博客 Docker MySQL 把数据存储在本地目录,很简单,只需要映射本地目录到容器即可 1.加上-v参数 ...
- 《一个民企CEO的职场阳谋》–读书总结(上)
职场是一个战场,很多人几十年在这里战斗. 职场是一个熔炉,很多人大半生在这里修炼. 如果在办公室里得不到快乐,生活就不会快乐. 如果公司里头感觉不到幸福,人生就不会幸福.(以上四句来自老刘的博客) & ...
- class的基本操作方法
JavaScript语言中,生成实例对象的传统方法是通过构造函数 function Point(x,y){ this.x = x; this.y = y; } Point.prototype.toSt ...
- 实现双向数据绑定mvvm
实现双向数据绑定mvvm
- Vue入门教程(2)
小白入门学习vue和vue实例,vue总结 这就是我脑海中的 Vue 知识体系: 一句话概况了 Vue 通过尽可能简单的 API 实现响应的数据绑定和组合的视图组件 Vue 的创建 我们的学习目的肯定 ...
- 【codeforces 501D】Misha and Permutations Summation
[题目链接]:http://codeforces.com/problemset/problem/501/D [题意] 给你两个排列; 求出它们的字典序num1和num2; 然后让你求出第(num1+n ...
- Sublime Text 3 Package Control安装
本系列文章由 @yhl_leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/50618314 安装好Sublime T ...
- stl里面stack的注意事项
1. pop是不返回元素的.因为不能返回引用,只能返回实例.而这个实例是在函数里面初始化的,所以必须在外面再赋值和初始化.而如果实例复制失败,会产生丢失. 2. 而top是可以返回引用的.实际上,返回 ...
- javascript 获取指定范围随机数
<script type="text/javascript"> function GetRandomNum(Min,Max){ var Range = Max - Mi ...
- Extjs4.2 ajax请求url中传中文參数乱码问题
今天有个需求须要在url中传入中文參数.结果在后台取得时出现乱码,怀疑可能是编码问题.上网查询了资料,试了几种办法.发现有一种可行,记录在此,以便查阅. url中用encodeURI 进行2次编码: ...