Questions tagged [for-await]

16 questions
57
votes
4 answers

for await of VS Promise.all

Is there any difference between this: const promises = await Promise.all(items.map(e => somethingAsync(e))); for (const res of promises) { // do some calculations } And this ? for await (const res of items.map(e => somethingAsync(e))) { // do…
NicoAdrian
  • 946
  • 1
  • 7
  • 18
5
votes
1 answer

javascript break in for-await loop finish the generator

I have written this code to iterate over github issues with a specific number (like pagination), in this case with 3 issues at once: const getUrl = (page) => `https://api.github.com/repos/angular/angular/issues?page=${page}`; const getIssues =…
Herbertusz
  • 1,151
  • 1
  • 11
  • 19
4
votes
1 answer

Dart - Await all async tasks in a for loop

I have a list of Objects in Dart and I want to perform an asynchronous operation with every one of these objects. After all of the asynchronous operations have finished, I want to perform a final operation. I am currently doing this with code that…
Geralt von Riva
  • 324
  • 3
  • 16
4
votes
1 answer

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. I'm using a "batchDelete" function from a DynamoDB ORM library which returns an AsyncIterableIterator type. According to the documentation here…
2
votes
1 answer

Chrome File System API hanging

disclaimer, self-answered post to hopefully save others time. Setup: I've been using chrome's implementation of the file systems API, [1] [2] [3]. This requires enabling the flag chrome://flags/#native-file-system-api. For starters I want to…
1
vote
2 answers

For-await loop inside Promise

Imagine we have an async generator function: async f * (connection) { while (true) { ... await doStuff() yield value } } Suppose that this function is virtually endless and gives us results of some async actions. We…
Tony I.
  • 498
  • 4
  • 16
1
vote
1 answer

How to declare type of an object created dinamicaly by for await in TypeScript

for await (account of accounts) { ... } Emit an error: "error TS2552: Cannot find name 'account'. Did you mean 'accounts'?" Thanks.
bbKid
  • 31
  • 6
1
vote
2 answers

Avoid using await in a for loop when Promise result updates the iterating loop variable itself

Is there any way to not use await inside loop for following code? const redirects = ['redirectId1']; for (let i = 0; i < redirects.length; i++) { const promiseResult = await anAsyncFunction(redirects[i]); if (promiseResult.redirects) { …
0
votes
0 answers

require function that uses for await loop

I have a file where I defined a function that uses a for await loop for reading a file: // updater.js const lineReader = require('readline').createInterface({ input: fs.createReadStream('./my-file.txt'), }) const updateFile = async () => { //…
Cereal Killer
  • 3,387
  • 10
  • 48
  • 80
0
votes
1 answer

JS for await...of equivalent in C#

My goal is to recreate a small slot machine, written in JavaScript, in C#. I am doing this to learn C#. I found the project on Github (user: asiryk, repository: slot-game). Except for the file ReelsContainer.ts, I have already managed to translate…
ImUseless
  • 1
  • 2
0
votes
1 answer

Why doesn't Javascript "await" wait for the correct amount of time?

This code doesn't work as expected: function sleep(ms) { return new Promise(resolve => setTimeout(resolve, ms)); } function diff(expected) { let x = expected - Date.now(); if (x > 0) { return `earlier ${x} ms`; } else if (x < 0) { …
ceremcem
  • 3,900
  • 4
  • 28
  • 66
0
votes
2 answers

For-await loop taking time for result

I am using for await loop to iterate through an array and match a value inside firestore cloud but unfortunately the result is not coming as expected; here is my code (async () => { for await (const element of array)…
0
votes
1 answer

Firebase Functions - Unexpected `await` inside a loop - Cannot read property 'reduce' of undefined

I am using Firebase Functions to pull in user data. Because there is a "IN" query limit of 10 for Firestore queries, I have to run an async callout in a loop. I can't do an async callout in a loop so I have to push the callouts synchronously into an…
Olivia
  • 1,843
  • 3
  • 27
  • 48
0
votes
1 answer

How to catch a rejected promise in a new expression node v14.4.0 "for await ... of"

I emulated 3 promises - two resolved, one rejected, wrapped a try-catch function, but I still get warnings in the console: (node: 4159) UnhandledPromiseRejectionWarning: Unhandled promise rejection. const emulate = (id, ms) => new…
Roman
  • 461
  • 4
  • 4
0
votes
0 answers

Avoid exiting a for-await loop when using asyn iterators

I am using this library https://www.npmjs.com/package/event-iterator to use async iterators. I have the following function export function grpcClientReadableStreamToAsyncIterator( stream: grpc.ClientReadableStream ): AsyncIterable { …
kosta
  • 4,302
  • 10
  • 50
  • 104
1
2