Skip to content

Commit

Permalink
Update promise.coroutine.md
Browse files Browse the repository at this point in the history
This is for this [issue](petkaantonov#1426)

TODO: The `Promise` by the function returned by the `coroutine` method needs further explanation. A thorough understanding of [the code](https://github.com/petkaantonov/bluebird/blob/4f9093448f55ea76d5a8e090e42fe24b8e0da82c/src/generators.js#L191) is needed.
  • Loading branch information
liushigit committed Jul 24, 2017
1 parent 4f90934 commit acca219
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions docs/docs/api/promise.coroutine.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ title: Promise.coroutine
Promise.coroutine(GeneratorFunction(...arguments) generatorFunction, Object options) -> function
```

Returns a function that can use `yield` to yield promises. Control is returned back to the generator when the yielded promise settles. This can lead to less verbose code when doing lots of sequential async calls with minimal processing in between. Requires node.js 0.12+, io.js 1.0+ or Google Chrome 40+.
In a typical use case, the parameter `generatorFunction` should be a `GeneratorFunction` producing a generator that yields one or more `Promise`s. (Also see the Tips section below.)

This method returns a function that returns a `Promise`(TODO: Elaborate this). When the returned function (e.g. the `ping` function in the example below) is called, it will start the generator (by calling its `next()`) and return immediately. Control is returned back to the generator when each yielded promise settles. This can lead to less verbose code when doing lots of sequential async calls with minimal processing in between. Requires node.js 0.12+, io.js 1.0+ or Google Chrome 40+.

```js
var Promise = require("bluebird");
Expand Down Expand Up @@ -52,8 +54,6 @@ Running the example:
Ping? 8
...

When called, the coroutine function will start an instance of the generator and returns a promise for its final value.

Doing `Promise.coroutine` is almost like using the C# `async` keyword to mark the function, with `yield` working as the `await` keyword. Promises are somewhat like `Task`s.

**Tip**
Expand Down

0 comments on commit acca219

Please sign in to comment.