stackp / promisejs

Lightweight javascript implementation of promises.
443 stars 127 forks source link

How can I chain ajax requests? #29

Closed peshoicov closed 7 years ago

peshoicov commented 7 years ago

I want to achieve something like this:

`promise.chain([

promise.get('some-url-1'),

promise.get('some-url-2'),

promise.get('some-url-3')

]).then(

function(err, n) {

    // get the responses here .. 

}

);`

Can you advise please.

stackp commented 7 years ago

Hi peshoicov, you probably want to use join() since you're not using the result of a previous call in the following ones:

promise.join([
    promise.get('/'),
    promise.get('/')
]).then(function(results) {
    console.log(results);
});

If for whatever reason you really want sequential execution, you have to pass a list of functions that return a promise:

promise.chain([
  function() {return promise.get('/');},
  function() {return promise.get('/');}
]).then(function(err, res) {
  console.log(res);
});