How to register a custom callback after Ember.RSVP.hash

I have a route that pulls 2 different promises using RSVP like

model: function() {
  return Ember.RSVP.hash(function() {
    stuff: App.Thing.find(),
    other: this.store.find('appointments', {day: day})    
  });
}

The problem is that I have a custom function that I bind / call in the β€œthen” of the second promise shown above. How can I call this when the callback comes along with another promise using RSVP?

Here is a custom callback as it stands today (and not inside RSVP.hash just yet)

    function bindContextAndGenerateAvailable(employee, other, appointments) {
        return function(filteredAppointments) {
            //will take the filtered collection and do something w/ it
            //to populate the bound appointments array passed in
        }
    }

    var appointments = Ember.A([]);
    this.store.find('appointment', {day: day}).then(function(response) {
        Ember.RSVP.all(response.getEach('employee')).then(function(empls){
          var filtered = response.filterBy('employee.id', employee);
          Ember.RSVP.resolve(filtered).then(bindContextAndGenerateAvailable(employee, other, appointments));
        });
    });
    return appointments;
+4
source share
2 answers

Well, I'm not sure if I fully understand, but I'm going to strike, and if I'm wrong, I will just edit it, because you say that my guesses are wrong.

Promises , (.then). , , , .

.

var foo = new Ember.RSVP.Promise(function(resolve){
  resolve(1);
}).then(function(result){
  return result + 2;
});

foo.then(function(result){
  alert(result); // this will alert 3, not 1
});

http://emberjs.jsbin.com/levoyama/1/edit

. , . : - - promise, .

var bar = Ember.RSVP.Promise.cast(1).then(function(result){
  return new Ember.RSVP.Promise(function(resolve){
    resolve(2 + result);
  });
});

bar.then(function(result){
  alert(result); // this will alert 3, not 1
});

http://emberjs.jsbin.com/voxevucu/1/edit

/ , , . 1 2 5, , 1/2.

var promise1 = Em.RSVP.Promise.cast(1)
                 .then(function(result){ return result + 1; })
                 .then(function(result){ return result + 1; })
                 .then(function(result){ return result + 1; })
                 .then(function(result){ return result + 1; });

var promise2 = Em.RSVP.Promise.cast(2)
                 .then(function(result){
                  return new Ember.RSVP.Promise(function(resolve){
                    resolve(result + 3);
                  });
                 });


var hash = Em.RSVP.hash({
   p1: promise1,
   p2: promise2
});

hash.then(function(result){
  alert(result.p1 + ' + ' + result.p2 + ' = ' + (result.p1 + result.p2) + ', alert the presses!!!!');
});

http://emberjs.jsbin.com/vevaruta/1/edit

promises/

. Promises , , , .

var promise1 = Em.RSVP.Promise.cast('of warcraft');

promise1.then(function(result){
  alert('hello world ' + result);
});

promise1.then(function(result){
  alert('stop playing world ' + result);
});

Em.RSVP.hash({
  p: promise1
}).then(function(hash){
  alert('hash result p is: ' + hash.p);
});

http://emberjs.jsbin.com/sovolibo/1/edit

, , ( ) Promises, - .

model: function(){
  var promise1 = Ember.RSVP.Promise.cast(1);
  var promise2 = Ember.RSVP.Promise.cast(2);

  return Ember.RSVP.hash({
    p1 = promise1,
    p2 = promise2
  }).then(function(result){
      return result.p1 + result.p2;
  });
}

3, , - .

- , , :

model: function(){
  var promise1 = Ember.RSVP.Promise.cast(1);
  var promise2 = Ember.RSVP.Promise.cast(2);

  var hash = Ember.RSVP.hash({
    p1 = promise1,
    p2 = promise2
  });

  var randomPromise = hash.then(function(result){
    return result.p1 + result.p2;
  });

  randomPromise.then(function(result){
    alert(result);
  });

  return hash;
}

, - , , .

+8

. :

var promise, colors = ['red', 'yellow', 'blue'];

promise = new Ember.RSVP.Promise(function(resolve, reject) {

  // timeouts below mimic a webservice response with new data
  setTimeout(function() {
    colors.pushObject('orange');
  }, 1000);

  setTimeout(function() {
    colors.pushObject('green');
  }, 2000);

  Ember.run.later(colors, function() {
    colors.pushObject('pink');
  }, 3000);

  resolve(colors);
});

return promise;

"", "" "", "", "" , , "". , .

+1

Source: https://habr.com/ru/post/1540717/


All Articles