ES6知识点整理之----async----异步遍历器

asyncIterator

ES2018 引入了”异步遍历器“(Async Iterator),为异步操做提供原生的遍历器接口,即valuedone这两个属性都是异步产生。javascript

异步遍历器的最大的语法特色,就是调用遍历器的next方法,返回的是一个 Promise 对象。java

asyncIterator
.next() .then( ({ value, done })
=> /* ... */ );

对象的异步遍历器接口,部署在Symbol.asyncIterator属性上面。无论是什么样的对象,只要它的Symbol.asyncIterator属性有值,就表示应该对它进行异步遍历。git

const asyncIterable = createAsyncIterable(['a', 'b']);
const asyncIterator = asyncIterable[Symbol.asyncIterator]();

asyncIterator
.next()
.then(iterResult1 => {
  console.log(iterResult1); // { value: 'a', done: false }
  return asyncIterator.next();
})
.then(iterResult2 => {
  console.log(iterResult2); // { value: 'b', done: false }
  return asyncIterator.next();
})
.then(iterResult3 => {
  console.log(iterResult3); // { value: undefined, done: true }
});

异步遍历器与同步遍历器最终行为是一致的,只是会先返回 Promise 对象,做为中介。github

因为异步遍历器的next方法,返回的是一个 Promise 对象。所以,能够把它放在await命令后面。数据结构

async function f() {
  const asyncIterable = createAsyncIterable(['a', 'b']);
  const asyncIterator = asyncIterable[Symbol.asyncIterator]();
  console.log(await asyncIterator.next());
  // { value: 'a', done: false }
  console.log(await asyncIterator.next());
  // { value: 'b', done: false }
  console.log(await asyncIterator.next());
  // { value: undefined, done: true }
}

异步遍历器的next方法是能够连续调用的,没必要等到上一步产生的 Promise 对象resolve之后再调用。这种状况下,next方法会累积起来,自动按照每一步的顺序运行下去。下面是一个例子,把全部的next方法放在Promise.all方法里面。异步

const asyncIterable = createAsyncIterable(['a', 'b']);
const asyncIterator = asyncIterable[Symbol.asyncIterator]();
const [{value: v1}, {value: v2}] = await Promise.all([
  asyncIterator.next(), asyncIterator.next()
]);

console.log(v1, v2); // a b

另外一种用法是一次性调用全部的next方法,而后await最后一步操做。async

async function runner() {
  const writer = openFile('someFile.txt');
  writer.next('hello');
  writer.next('world');
  await writer.return();
}

runner();

for await...of

用于遍历异步的 Iterator 接口。函数

async function f() {
  for await (const x of createAsyncIterable(['a', 'b'])) {
    console.log(x);
  }
}
// a
// b

for await...of循环的一个用途,是部署了 asyncIterable 操做的异步接口,能够直接放入这个循环。spa

若是next方法返回的 Promise 对象被rejectfor await...of就会报错,要用try...catch捕捉。设计

注意,for await...of循环也能够用于同步遍历器。

异步 Generator 函数

返回一个异步遍历器对象。

在语法上,异步 Generator 函数就是async函数与 Generator 函数的结合。

async function* gen() {
  yield 'hello';
}
const genObj = gen();
genObj.next().then(x => console.log(x));
// { value: 'hello', done: false }

异步遍历器的设计目的之一,就是 Generator 函数处理同步操做和异步操做时,可以使用同一套接口。

异步 Generator 函数能够与for await...of循环结合起来使用。

async function* prefixLines(asyncIterable) {
  for await (const line of asyncIterable) {
    yield '> ' + line;
  }
}

注意,普通的 async 函数返回的是一个 Promise 对象,而异步 Generator 函数返回的是一个异步 Iterator 对象。

javascript如今一共有4种函数形式:

  • 普通函数
  • async 函数
  • Generator 函数
  • 异步 Generator 函数

若是是一系列按照顺序执行的异步操做(好比读取文件,而后写入新内容,再存入硬盘),可使用 async 函数;

若是是一系列产生相同数据结构的异步操做(好比一行一行读取文件),可使用异步 Generator 函数。

同步的数据结构,也可使用异步 Generator 函数。

yield* 语句

yield*语句也能够跟一个异步遍历器。

async function* gen1() {
  yield 'a';
  yield 'b';
  return 2;
}

async function* gen2() {
  // result 最终会等于 2
  const result = yield* gen1();
}

与同步 Generator 函数同样,for await...of循环会展开yield*

(async function () {
  for await (const x of gen2()) {
    console.log(x);
  }
})();
// a
// b
相关文章
相关标签/搜索