Skip to content

Instantly share code, notes, and snippets.

@shyshy
Last active October 17, 2018 13:48
Show Gist options
  • Save shyshy/d2393787d9fe734184887108b7415954 to your computer and use it in GitHub Desktop.
Save shyshy/d2393787d9fe734184887108b7415954 to your computer and use it in GitHub Desktop.
ember-concurrency finally
import Ember from 'ember';
import { task, timeout } from 'ember-concurrency';
import { run } from '@ember/runloop';
export default Ember.Controller.extend({
runs: [],
test: task(function*() {
const id = Math.floor(Math.random() * 1000);
// yield timeout(1);
try {
this.get('runs').pushObject({ id, message: 'try' });
yield timeout(3000);
} finally {
this.get('runs').pushObject({ id, message: 'finally' });
}
this.get('runs').pushObject({ id, message: 'completed' });
}).restartable(),
actions: {
test() {
this.get('test').perform();
},
clear() {
this.get('test').cancelAll();
run.later(() => { this.get('runs').clear(); }, 1);
}
}
});
<br>
{{outlet}}
<label for="test-thing">Click the button many times to restart task</label>
<div><button id="test-thing" {{action 'test'}}>click</button></div>
<h4>Runs</h4>
<button {{action 'clear'}}>Clear Runs</button>
{{#each runs as |run|}}
<li>{{run.id}}: {{run.message}}</li>
{{/each}}
<br>
<br>
<p>The canceled's task's <code>finally</code> runs <i><b>after</b></i> the next task's <code>try</code> block.</p>
<p>Shouldn't the canceled task's <code>finally</code> block run <i><b>before</b></i> the next task's <code>try</code> block?</p>
{
"version": "0.15.1",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js",
"ember": "3.4.3",
"ember-template-compiler": "3.4.3",
"ember-testing": "3.4.3"
},
"addons": {
"ember-data": "3.4.2",
"ember-concurrency": "0.8.22"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment