Skip to content

Instantly share code, notes, and snippets.

@vasilionjea
Last active August 14, 2018 02:49
Show Gist options
  • Save vasilionjea/af7c25e8939a66416d5a1ac5af3978d5 to your computer and use it in GitHub Desktop.
Save vasilionjea/af7c25e8939a66416d5a1ac5af3978d5 to your computer and use it in GitHub Desktop.
Ember Conccurency - Error Handling
import Ember from 'ember';
import { task } from 'ember-concurrency';
function fetchReject(type) {
return new Promise(function(resolve, reject) {
Ember.run.later(() => reject('rejected reason'), 1000);
});
}
export default Ember.Controller.extend({
appName: 'Ember Conccurency - Error Handling',
actions: {
myTask() {
this.get('myTask')
.perform()
.then(() => console.log('success: task is a success'))
.catch(e => console.log('catch 2:', e));
},
myBuggyTask() {
this.get('myBuggyTask')
.perform()
.then(() => console.log('success: task is a success'))
.catch(e => console.log('catch 2:', e));
},
},
myTask: task(function*() {
try {
const response = yield fetchReject();
console.log(response);
} catch (e) {
console.log('catch 1:', e);
// You must do one of these if you want the error to get
// "piped" through the promise chain in the action.
//
// yield Promise.reject(e);
// return Promise.reject(e);
throw e;
}
}).drop(),
myBuggyTask: task(function*() {
try {
const response = yield fetchReject();
} catch (e) {
console.log('catch 1:', e);
}
}).drop(),
});
<h1>{{appName}}</h1>
<hr>
<p>
<button {{action "myTask"}}>Submit my task</button>
</p>
<p>
<button {{action "myBuggyTask"}}>Submit my buggy task</button>
</p>
<p>Click the buttons and check your console.</p>
{
"version": "0.15.0",
"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.2.2",
"ember-template-compiler": "3.2.2",
"ember-testing": "3.2.2"
},
"addons": {
"ember-concurrency": "0.8.19"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment