Note that there are some explanatory texts on larger screens.

plurals
  1. POCoffeescript with callbacks & simplified error handling
    text
    copied!<p>I would like to be able to refactor the error handling from this coffeescript code:</p> <pre class="lang-coffee prettyprint-override"><code># Do some stuff with 2 levels of asynchronous callbacks and error handling vote = (res, data) -&gt; Case.findOne { caseId: data.id }, (err, mycase) -&gt; if err console.error 'Vote failed' else myvote = new Vote case: mycase._id myvote.save (err) -&gt; if err console.error 'Could not add vote' else console.log 'Success!' </code></pre> <p>to something like this:</p> <pre class="lang-coffee prettyprint-override"><code># Run my function, do error handling, and run the callback if no error runit = (func, arg, errmsg, callback) -&gt; func arg, (err, docs) -&gt; if err console.log errmsg + ': ' + err else callback docs # Original code, simplified vote = (res, data) -&gt; runit Case.findOne { caseId: data.id }, 'Vote failed', (mycase) -&gt; myvote = new Vote case: mycase._id runit myvote.save, 'Could not add vote', () -&gt; console.log 'Success!' </code></pre> <p>Obviously, the <code>runit</code> function needs be able to handle one or more arguments correctly, which I didn't attempt to code correctly.</p> <p>If I run it like this, I get an error:</p> <pre><code>node.js:201 throw e; // process.nextTick error, or 'error' event on first tick ^ TypeError: Cannot read property 'findOne' of undefined at /tmp/node_modules/mongoose/node_modules/hooks/hooks.js:27:28 at /tmp/lib/api.js:227:12 at Promise.&lt;anonymous&gt; (/tmp/lib/api.js:216:16) at Promise.&lt;anonymous&gt; (/tmp/node_modules/mongoose/lib/promise.js:120:8) at Promise.&lt;anonymous&gt; (events.js:67:17) at Promise.emit (/tmp/node_modules/mongoose/lib/promise.js:59:38) at Promise.complete (/tmp/node_modules/mongoose/lib/promise.js:70:20) at /tmp/node_modules/mongoose/lib/query.js:885:15 at model.&lt;anonymous&gt; (/tmp/node_modules/mongoose/lib/document.js:181:5) at model.init (/tmp/node_modules/mongoose/lib/model.js:181:36) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload