-
Improvement
-
Resolution: Fixed
-
Major
-
3.2
-
MOODLE_32_STABLE
-
MOODLE_34_STABLE
-
MDL-57139-master -
I have discussed this with integrators as from my own experience the eslint-plugin-promise helped me fix some of my own misunderstandings of promises and I think it would be a useful addition to our ruleset.
https://pouchdb.com/2015/05/18/we-have-a-problem-with-promises.html gives a great bit of background of the use of the point lints.
Also:
http://taoofcode.net/promise-anti-patterns/
https://github.com/petkaantonov/bluebird/wiki/Promise-anti-patterns
http://www.datchley.name/promise-patterns-anti-patterns/
Rule: catch-or-return
Ensure that each time a then() is applied to a promise, a catch() is applied as well. Exceptions are made if you are returning that promise.
Valid
myPromise.then(doSomething).catch(errors);
|
myPromise.then(doSomething).then(doSomethingElse).catch(errors);
|
function doSomethingElse() { return myPromise.then(doSomething) }
|
Invalid:
myPromise.then(doSomething);
|
myPromise.then(doSomething, catchErrors); // catch() may be a little better
|
function doSomethingElse() { myPromise.then(doSomething) }
|
Rule: always-return
Ensure that inside a then() you make sure to return a new promise or value. See http://pouchdb.com/2015/05/18/we-have-a-problem-with-promises.html (rule #5) for more info on why that's a good idea.
We also allow someone to throw inside a then() which is essentially the same as return Promise.reject().
Valid
myPromise.then((val) => val * 2));
|
myPromise.then(function(val) { return val * 2; });
|
myPromise.then(doSomething); // could be either
|
myPromise.then((b) => { if (b) { return "yes" } else { return "no" } });
|
Invalid:
myPromise.then(function(val) {});
|
myPromise.then(() => { doSomething(); });
|
myPromise.then((b) => { if (b) { return "yes" } else { forgotToReturn(); } });
|