Hiển thị các bài đăng có nhãn Chaining http requests. Hiển thị tất cả bài đăng
Hiển thị các bài đăng có nhãn Chaining http requests. Hiển thị tất cả bài đăng

Chain multiple Node http request

Source: https://stackoverflow.com/questions/34835940/chain-multiple-node-http-request

Firstly, you'll want to take a look at request, which is most popular choice for HTTP requests, due to its simplicity.
Secondly, we can combine the simplicity of request with the concept of Promises, to make multiple requests in succession, while keeping the code flat.
Using request-promise
var rp = require('request-promise')
var url1 = {}
var url2 = {}
var url3 = {}


rp(url1)
  .then(response => {
    // add stuff from url1 response to url2
    return rp(url2)
  })
  .then(response => {
    // add stuff from url2 response to url3
    return rp(url3)
  })
  .then(response => {
    // do stuff after all requests

    // If something went wrong
    // throw new Error('messed up')
  })
  .catch(err => console.log) // Don't forget to catch errors
As you can see, we can add as many requests as we want, and the code will stay flat and simple. As a bonus, we were able to add error-handling as well. Using traditional callbacks, you'd have to add error-handling to every callback, whereas here we only have to do it once at the end of the Promise chain.
UPDATE (09/16): While Promises take us halfway there, further experience has convinced me that Promises alone get messy when there is a lot of mixing between sync, async code, and especially control flow (e.g. if-else). The canonical way to solve this would be with async/await, however that is still in development and would require transpilation. As such, generators are the next best solution.
Using co
var co = require('co')
var rp = require('request-promise')
var url1 = {}
var url2 = {}
var url3 = {}

co(function* () {
  var response
  response = yield rp(url1)
  // add stuff from url1 response to url2
  response = yield rp(url2)
  // add stuff from url2 response to url3
  response = yield rp(url3)

  // do stuff after all requests

  // If something went wrong
  // throw new Error('messed up')
})
.catch(err => console.log) // Don't forget to catch errors
UPDATE (12/16): Now that the latest version of node at time of writing (7.2.1) supports async/await behind the --harmony flag, you could do this:
const rp = require('request-promise')
const url1 = {}
const url2 = {}
const url3 = {}

async function doRequests() {
  let response
  response = await rp(url1)
  // add stuff from url1 response to url2
  response = await rp(url2)
  // add stuff from url2 response to url3
  response = await rp(url3)

  // do stuff after all requests

  // If something went wrong
  // throw new Error('messed up')
}

doRequests()
.catch(err => console.log) // Don't forget to catch errors

Angular 2 - Chaining http requests

Source: https://stackoverflow.com/questions/42626536/angular-2-chaining-http-requests
https://stackoverflow.com/questions/49002681/send-multiple-requests-in-sequence-in-angular-5-resolver


Chaining HTTP requests can be achieved using the Observable.flatMap operator. Say we want to make three requests where each request depends on the result of previous one:
this.service.firstMethod()
    .flatMap(firstMethodResult => this.service.secondMethod(firstMethodResult))
    .flatMap(secondMethodResult => this.service.thirdMethod(secondMethodResult))
    .subscribe(thirdMethodResult => {
          console.log(thirdMethodResult);
     });
This way you can chain as much interdependent requests you want

I had a similar problem where I ended up using flatMap callback, basically from the documentation, it allows us to perform sequential tasks and return observable in the end.
You can try something like in the resolve function of the resolver,
return request1().flatMap( (response: any) => {
    return forkJoin( [ request2(), request3() ];
} );
Where request1 is the function that you want to be performed before request2 and request3 are executed. It goes without saying that, all the requests must be returning Observables.
More: https://coryrylan.com/blog/angular-multiple-http-requests-with-rxjs

Renewing Facebook Graph API token automatically?

  Mã truy cập dài hạn https://developers.facebook.com/docs/facebook-login/guides/access-tokens/get-long-lived/ https://community.n8n.io/t/re...