Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

async/await #42

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions docs/common-pitfalls/async-operations-loops.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,26 @@ console.dir(results)

This has the same problems as before: Nightmare is executing multiple queues against the same instance. The above has a new wrinkle in that there could be an arbitrary nummber of URLs to go to. The results will likely be empty as none of the Nightmare chains have had time to finish executing.

### Async / await

The new async syntax can be simplify this. The loop waits for each promise to resolve.

```js
var urls = ['http://example.com', 'http://example2.com', 'http://example3.com'];

var results = []

async function collectTitles(urls){
for ( url of urls ) {
results.push(await nightmare.goto(url).wait('body').title())
}
await nightmare.end()
console.dir(results)
}

collectTitles(urls)
```

### Vanilla JS
A variable number of queues can be solved through `Array.reduce`. Consider:

Expand Down