https://github.com/suissa/request-promise-chains
How chain requests with promises
https://github.com/suissa/request-promise-chains
Last synced: about 2 months ago
JSON representation
How chain requests with promises
- Host: GitHub
- URL: https://github.com/suissa/request-promise-chains
- Owner: suissa
- License: mit
- Created: 2016-10-05T15:47:17.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-05-13T02:53:22.000Z (about 8 years ago)
- Last Synced: 2025-03-21T15:12:30.631Z (2 months ago)
- Language: JavaScript
- Size: 2.93 KB
- Stars: 13
- Watchers: 1
- Forks: 3
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# request-promise-chains
**How chain requests with promises!**
It´s pretty easy, look:
```js
const request = require('request')
const rp = require('request-promise')const options = {
uri: '',
headers: {
'User-Agent': 'Suissa-Chain-of-Requests'
},
json: true
}
const url1 = options
const url2 = options
const url3 = optionsconst user = 'suissa'
url1.uri = 'https://api.github.com/users/' + userconst cb1 = (response) => {
console.log('1) Dados do usuário '+ response.name +': ', response)
url2.uri = response.organizations_url
return rp(url2)
}
const cb2 = (response) => {
console.log('2) Listagem das suas organizações: ')
response.forEach((org, i) => {
console.log('- ' + org.login)
if (org.login.toLowerCase() === 'webschool-io')
url3.uri = org.repos_url
})
return rp(url3)
}const cb3 = (response) => {
console.log('3) Quantidade dos repos da org (por page): ', response[0].owner.login)
console.log(response.length)
}rp(url1)
.then(cb1)
.then(cb2)
.then(cb3)
.catch(err => console.log)// FIM
```As you can see you need only return another request-promise with correct options and DONE!