Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Throwing from within the function that returns the promise will occur before the promise is returned, so, no:</p> <pre><code>function foo() { var status = _.Deferred(); throw 'DOH!'; return status.promise(); // this will never execute } </code></pre> <p>Where you run into trouble is if async calls initiated in the promise-returning function can throw:</p> <pre><code>function bar() { throw 'DOH!'; } function foo() { var status = _.Deferred(); setTimeout(function () { status.resolve(bar()); }, 0); return status.promise(); } foo(). fail(function () { // *not* invoked when bar throws }); </code></pre> <p>In this case, you would need to wrap the call to <code>bar</code>:</p> <pre><code>function bar() { throw 'DOH!'; } function foo() { var status = _.Deferred(); setTimeout(function () { try { status.resolve(bar()); } catch (e) { status.reject(e); } }, 0); return status.promise(); } foo(). fail(function () { // invoked when bar throws }); </code></pre> <p>However, async functions that accept a callback should catch their own errors, and pass the result or error to the callback.</p> <p>If the async functions instead return promises, you do not need to catch and in fact do not need to create your own <code>Deferred</code>:</p> <pre><code>function bar() { var d = _.Deferred(); setTimeout(function () { d.resolve(42); }, 0); return d.promise(); } function foo() { return bar(). then(function (result) { return result * 2; }). then(function (result) { if (result === 84) { // true throw 'DOH!'; } )); } foo(). then(function (result) { // *not* invoked }). fail(function (e) { console.log(e.message); // 'DOH!' }); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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