Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p>To me, this result doesn't make sense. By attaching to this promise chain, each then is implying the intent that it will be dependant upon the successful resolution of d1 and a result being passed down the chain</p> </blockquote> <p>No. What you are describing is not a chain, but just attaching all the callbacks to <code>d1</code>. Yet, if you want to chain something with <code>then</code>, the result for <code>promise2</code> is dependent on the resolution of <code>promise1</code> <em>and how the <code>then</code> callbacks handled it</em>.</p> <p>The docs state:</p> <blockquote> <p>Returns a new promise for the result of the callback(s).</p> </blockquote> <p>The <code>.then</code> method is usually looked upon in terms of the <a href="http://wiki.commonjs.org/wiki/Promises/A" rel="noreferrer">Promises/A specification</a> (or the even stricter <a href="http://promises-aplus.github.io/promises-spec/" rel="noreferrer">Promsises/A+ one</a>). That means the callbacks shell return promises which will be assimilated to become the resolution of <code>promise2</code>, and if there is no success/error handler the respective result will in case be passed directly to <code>promise2</code> - so you can simply <strong>omit the handler</strong> to propagate the error.</p> <p>Yet, if the error is <em>handled</em>, the resulting <code>promise2</code> is seen as fixed and will be fulfilled with that value. If you don't want that, you would have to <strong>re-<code>throw</code> the error</strong>, just like in a try-catch clause. Alternatively you can return a (to-be-)rejected promise from the handler. Not sure what Dojo way to reject is, but:</p> <pre><code>var d1 = d(); var promise1 = d1.promise.then( function(wins) { console.log('promise1 resolved'); return wins;}, function(err) { console.log('promise1 rejected'); throw err;}); var promise2 = promise1.then( function(wins) { console.log('promise2 resolved'); return wins;}, function(err) { console.log('promise2 rejected'); throw err;}); var promise3 = promise2.then( function(wins) { console.log('promise3 resolved'); return wins;}, function(err) { console.log('promise3 rejected'); throw err;}); d1.reject(new Error()); </code></pre> <blockquote> <p>How is Bob able to get a blue widget from Ginger when didn't get any herself?</p> </blockquote> <p>He should not be able. If there are no error handlers, he will just perceive the message (((from the distributor) from John) from Ginger) that there are no widgets left. Yet, if Ginger sets up an error handler for that case, she still might fulfill her promise to give Bob a widget by giving him a green one from her own shack if there are no blue ones left at John or his distributor.</p> <p>To translate your error callbacks into the metapher, <code>return err</code> from the handler would just be like saying "if there are no widgets left, just give him the note that there are no ones left - it's as good as the desired widget".</p> <blockquote> <p>In the database situation, if the db.query failed, it would call the err function of the first then</p> </blockquote> <p>…which would mean that the error is handled there. If you don't do that, just omit the error callback. Btw, your success callbacks don't <code>return</code> the promises they are creating, so they seem to be quite useless. Correct would be:</p> <pre><code>var promise = db.query({parent_id: value}); promise.then(function(query_result) { var first_value = { parent_id: query_result[0].parent_id } var promise = db.put(first_value); return promise.then(function(first_value_result) { var second_value = { reference_to_first_value_id: first_value_result.id } var promise = db.put(second_value); return promise.then(function(second_value_result) { return values_successfully_entered(); }); }); }); </code></pre> <p>or, since you don't need the closures to access result values from previous callbacks, even:</p> <pre><code>db.query({parent_id: value}).then(function(query_result) { return db.put({ parent_id: query_result[0].parent_id }); }).then(function(first_value_result) { return db.put({ reference_to_first_value_id: first_value_result.id }); }.then(values_successfully_entered); </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