https://github.com/kanitsharma/yieldpromise
Yield promises in generator functions
https://github.com/kanitsharma/yieldpromise
async generator iterables javascript promises
Last synced: 11 months ago
JSON representation
Yield promises in generator functions
- Host: GitHub
- URL: https://github.com/kanitsharma/yieldpromise
- Owner: kanitsharma
- Created: 2018-05-04T18:23:33.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2018-07-08T20:56:29.000Z (almost 8 years ago)
- Last Synced: 2025-02-07T13:44:07.443Z (over 1 year ago)
- Topics: async, generator, iterables, javascript, promises
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/yieldpromise
- Size: 99.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# yieldpromise
Yield promises in generator functions
## Getting started
```javascript
npm i -s yieldpromise
```
### Usage
```javascript
const run = require("yieldpromise");
const fetch = require("node-fetch");
// Using as a standalone function
run(function* main() {
const a = yield fetch("https://jsonplaceholder.typicode.com/posts/1");
const b = yield a.json();
console.log(b);
});
```
#### Since run injects resolve and reject as parameters to the child generator function, it can be used inside promise chain
##### Example
```javascript
const run = require("yieldpromise");
const fetch = require("node-fetch");
run(function* main(resolve, reject) {
const a = yield fetch("https://jsonplaceholder.typicode.com/posts/1");
const b = yield a.json();
resolve(b);
})
.then(x => x.userId)
.then(console.log);
```
##### OR
```javascript
const run = require("yieldpromise");
const fetch = require("node-fetch");
fetch("https://jsonplaceholder.typicode.com/posts/1")
.then(x =>
run(function* main(resolve, reject) {
const a = yield x.json();
const b = a.userId;
resolve(b);
})
)
.then(console.log);
```