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

Use of await and yield #66

Merged
merged 7 commits into from
Nov 8, 2017
Merged
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
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,32 @@ newScore //=> 57

As you can see, because the pipe operator always pipes a single result value, it plays very nicely with the single-argument arrow function syntax. Also, because the pipe operator's semantics are pure and simple, it could be possible for JavaScript engines to optimize away the arrow function.

### Use of `await`

The pipeline operator allows a `Promise` to be `await`ed as follows:

```js
promise |> await
```

which is the equivalent of

```js
await promise
```

This is to allow you to `await` the result of an asynchronous function and pass it to the next function from within a function pipeline, as follows:

```js
const userAge = userId |> fetchUserById |> await |> getAgeFromUser
```

which is the equivalent of

```js
const userAge = getAgeFromUser(await fetchUserById(userId))
```

### Usage with Function.prototype.papp

If the [papp proposal](https://github.com/mindeavor/es-papp) gets accepted, the pipeline op would be even easier to use. Rewriting the previous example:
Expand Down