简体   繁体   English

调用 function 返回 AsyncIterableIterator 而不使用“for await”块

[英]Calling a function that returns a AsyncIterableIterator without using “for await” block

I'm writing an AWS Lambda function in TypeScript using the Node.js runtime.我正在使用 Z3B2819DD4C24EDA2FAF2052EEF4 运行时在 TypeScript 中编写 AWS Lambda function。 I'm using a "batchDelete" function from a DynamoDB ORM library which returns an AsyncIterableIterator type.我正在使用来自 DynamoDB ORM 库的“batchDelete”function,它返回AsyncIterableIterator类型。

According to the documentation here https://github.com/awslabs/dynamodb-data-mapper-js#batchDelete , I should invoke the method with a for await loop like this:根据此处的文档https://github.com/awslabs/dynamodb-data-mapper-js#batchDelete ,我应该使用 for await 循环调用该方法,如下所示:

for await (const found of mapper.batchDelete(toRemove)) {
    // items will be yielded as they are successfully removed
}

This all works great but the problem comes in where if I enable ESLint on my project.这一切都很好,但是如果我在我的项目上启用ESLint ,问题就出在哪里。 The default rules throw an error because the for await block is empty.默认规则会引发错误,因为 for await 块是空的。 I also get a warning because the found constant is never used.我也收到警告,因为找到的常量从未使用过。 I have no use for the found constant and don't want to log it.我对找到的常量没有用处,也不想记录它。 I was wondering if there was another way to call an AsyncIterableIterator function where we disregard what is returned and don't have the empty block?我想知道是否有另一种方法来调用AsyncIterableIterator function 我们忽略返回的内容并且没有空块?

If you don't care about the results of the iteration, then you should probably just do something like this:如果您不关心迭代的结果,那么您可能应该这样做:

await Promise.all(toRemove.map(item => mapper.delete(item));

To use the mapper.batchDelete(toRemove) result more directly, you have to allow for multiple levels of promises.要更直接地使用mapper.batchDelete(toRemove)结果,您必须允许多个级别的 Promise。 Perhaps you could do this:也许你可以这样做:

await Promise.all(await mapper.batchDelete(toRemove)[Symbol.asyncIterator]());

In doing this, await mapper.batchDelete(toRemove)[Symbol.asyncIterator]() , that would get you the default async Iterator and then passing it to Promise.all() would iterate it to get an iterable of promises.在执行此操作时, await mapper.batchDelete(toRemove)[Symbol.asyncIterator]()将为您提供默认的异步迭代器,然后将其传递给Promise.all()将对其进行迭代以获得可迭代的承诺。 Unfortunately, in building it to make this easier:不幸的是,在构建它以使其更容易:

for await (const found of mapper.batchDelete(toRemove))

they made it a bit more difficult to just get an array of promises out of it.他们使从中获得一系列承诺变得更加困难。

FYI, here's a link to the code for the .batchDelete() method if you want to look at how it's implemented.仅供参考,如果您想了解它是如何实现的,这里是.batchDelete()方法的代码链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM