Note:

If you want to create a new page for developers, you should create it on the Moodle Developer Resource site.

User talk:Poltawski/Javascript promises: Difference between revisions

From MoodleDocs
(Created page with " == Promises == Promises are used extensively in modern Moodle Javascript APIs to handle asynchronous situations. It is unfortunately common to misunderstand how they operat...")
 
Line 3: Line 3:




Promises are used extensively in modern Moodle Javascript APIs to handle asynchronous situations. It is unfortunately common to misunderstand how they operate and introduce bugs which only expose themselves in asynchronous edge cases (see article [https://pouchdb.com/2015/05/18/we-have-a-problem-with-promises.html we have a problem with promises]). In order to make use of promises in Moodle more understandable, consistent and avoid edge case bugs, we have adopted best practices suggested by Nolan Lawson and verified by [https://github.com/xjamundx/eslint-plugin-promise eslint-plugin-promise].
Promises are used extensively in modern Moodle Javascript APIs to handle asynchronous situations. It is unfortunately common to misunderstand how they operate and introduce bugs which only expose themselves in asynchronous edge cases (see article [https://pouchdb.com/2015/05/18/we-have-a-problem-with-promises.html we have a problem with promises]). In order to make best use of promises in Moodle more understandable, consistent and avoid edge case bugs, we have adopted best practices suggested by Nolan Lawson and verified by [https://github.com/xjamundx/eslint-plugin-promise eslint-plugin-promise].


=== Summary ===
<blockquote>
<blockquote>
What can we do here? There are three things:
What can we do here? There are three things:
Line 11: Line 12:
* throw a synchronous error
* throw a synchronous error
</blockquote>
</blockquote>
=== Always return or throw  ===
Always return in promises. Seee https://pouchdb.com/2015/05/18/we-have-a-problem-with-promises.html #5
=== Do not nest ===
The great advantage of promises is keeping async code linear rather than nested in <i>callback hell</i> and handling errors in one place, by nesting promises these advantages are lost.

Revision as of 10:14, 1 June 2017

Promises

Promises are used extensively in modern Moodle Javascript APIs to handle asynchronous situations. It is unfortunately common to misunderstand how they operate and introduce bugs which only expose themselves in asynchronous edge cases (see article we have a problem with promises). In order to make best use of promises in Moodle more understandable, consistent and avoid edge case bugs, we have adopted best practices suggested by Nolan Lawson and verified by eslint-plugin-promise.

Summary

What can we do here? There are three things:

  • return another promise
  • return a synchronous value (or undefined)
  • throw a synchronous error

Always return or throw

Always return in promises. Seee https://pouchdb.com/2015/05/18/we-have-a-problem-with-promises.html #5


Do not nest

The great advantage of promises is keeping async code linear rather than nested in callback hell and handling errors in one place, by nesting promises these advantages are lost.