Skip to content

Instantly share code, notes, and snippets.

@manufitoussi
Created January 5, 2018 11:09
Show Gist options
  • Save manufitoussi/b12437e7734951efbae796c2493b2316 to your computer and use it in GitHub Desktop.
Save manufitoussi/b12437e7734951efbae796c2493b2316 to your computer and use it in GitHub Desktop.
Test enqueue promises
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle'
});
import Ember from 'ember';
Ember.RSVP.queue = (promises) => {
return promises.reduce((previous, current) => {
return () => previous().then(current);
}, () => Ember.RSVP.resolve())();
};
export default Ember.Route.extend({
myPromise() {
var result = '';
var p = (text) => {
return () => new Ember.RSVP.Promise((resolve, reject) => {
var time = Math.round(Math.random()*1000);
setTimeout(() => {
console.log(text, time);
result += `<li>${text}</li>`;
resolve(result);
}, time);
});
};
var promises = [
p(1),
p(2),
p(3),
p(4),
p(5),
];
return Ember.RSVP.queue(promises);
},
model() {
return this.myPromise().then((result) => {
console.log('fin!');
result+= `<li>fin!</li>`;
return result;
});
}
});
<h1>Welcome to {{appName}}</h1>
<br>
<br>
{{outlet}}
<br>
<br>
<p>Results</p>
<ul>
{{{model}}}
</ul>
{
"version": "0.13.0",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "2.16.2",
"ember-template-compiler": "2.16.2",
"ember-testing": "2.16.2"
},
"addons": {
"ember-data": "2.16.3"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment