Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 749 Vote(s) - 3.57 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using async/await with a forEach loop

#21
As other answers have mentioned, you're probably wanting it to be executed in sequence rather in parallel. Ie. run for first file, wait until it's done, *then* once it's done run for second file. That's not what will happen.

I think it's important to address *why* this doesn't happen.

Think about how `forEach` works. I can't find the source, but I presume it works something like this:

```
const forEach = (arr, cb) => {
for (let i = 0; i < arr.length; i++) {
cb(arr[i]);
}
};
```

Now think about what happens when you do something like this:

```
forEach(files, async logFile(file) {
const contents = await fs.readFile(file, 'utf8');
console.log(contents);
});
```

Inside `forEach`'s `for` loop we're calling `cb(arr[i])`, which ends up being `logFile(file)`. The `logFile` function has an `await` inside it, so maybe the `for` loop will wait for this `await` before proceeding to `i++`?

No, it won't. Confusingly, that's not how `await` works. From [the docs][1]:

> An await splits execution flow, allowing the caller of the async function to resume execution. After the await defers the continuation of the async function, execution of subsequent statements ensues. If this await is the last expression executed by its function execution continues by returning to the function's caller a pending Promise for completion of the await's function and resuming execution of that caller.

So if you have the following, the numbers won't be logged before `"b"`:

```
const delay = (ms) => {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
};

const logNumbers = async () => {
console.log(1);
await delay(2000);
console.log(2);
await delay(2000);
console.log(3);
};

const main = () => {
console.log("a");
logNumbers();
console.log("b");
};

main();
```

Circling back to `forEach`, `forEach` is like `main` and `logFile` is like `logNumbers`. `main` won't stop just because `logNumbers` does some `await`ing, and `forEach` won't stop just because `logFile` does some `await`ing.

[1]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through