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);
});
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);
});
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;
}
, - , , .