Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>var p1 = someFunction(); var p2 = p1.then(successCallback); p2.fail(errorCallback); </code></pre> <blockquote> <p>So my question becomes what is the relationship between the promises p1 and p2 in the example above. How does that relate to the chain idea?</p> </blockquote> <p>Promises are a way of handling asynchronous code, here's a <a href="https://tutsplus.com/lesson/deferreds/" rel="nofollow">quick video</a> and <a href="http://net.tutsplus.com/tutorials/javascript-ajax/wrangle-async-tasks-with-jquery-promises/" rel="nofollow">article</a> that explains them. <code>someFunction()</code> is going to do an async task, when it finishes it (i.e. gets <a href="http://api.jquery.com/deferred.resolve/" rel="nofollow">resolved</a>) it executes the <code>successCallback</code> and if that fails (ie. is <a href="http://api.jquery.com/deferred.resolve/" rel="nofollow">rejected</a>) it executes the <code>errorCallback</code>. The relationship is that <code>p2</code> is a promise that will get executed only when <code>p1</code> gets <a href="http://api.jquery.com/deferred.reject/" rel="nofollow">rejected</a>. Think of <code>p1.then(win, fail)</code> and <code>p1.then(win).fail(fail)</code> as two ways of writing the same thing.</p> <p>The original question was probably referring to the following:</p> <blockquote> <p>A promise object is returned after a JSONStore asynchronous operation is called (find, add, remove, and so on). [...] The [JSONStore.add, JSONStore.find, etc.] failure callback, passed as either the second parameter of .then or the first parameter of .fail returns an error object, [...]</p> </blockquote> <p>from the <a href="http://pic.dhe.ibm.com/infocenter/wrklight/v5r0m6/index.jsp?topic=%2Fcom.ibm.worklight.help.doc%2Fapiref%2Fr_class_wl_jsonstore.html" rel="nofollow">JSONStore Documentation</a>. The documentation is specific to the JSONStore API, we refer readers to the jQuery documentation to learn about how promises work in general (<em>see jQuery's API documentation for more details.</em>).</p> <p>Here's an example:</p> <pre><code>var collections = { people : { searchFields : {name: 'THIS_SHOULD_PRODUCE_AN_ERROR'} } }; WL.JSONStore.init(collections) .then(function(res){ //the code here should not be called because we expect to fail console.log(typeof res === 'object' ? JSON.stringify(res) : res); }) .fail(function(err){ console.log(err.toString()); }); </code></pre> <p>The output is:</p> <pre><code>{"src":"initCollection","err":-12,"msg":"INVALID_SEARCH_FIELD_TYPES","col":"people","usr":"jsonstore","doc":{},"res":{}} </code></pre> <p>If we switch to <code>.then(success, failure)</code>:</p> <pre><code>var collections = { people : { searchFields : {name: 'THIS_SHOULD_PRODUCE_AN_ERROR'} } }; var success = function(res){ //the code here should not be called because we expect to fail console.log(typeof res === 'object' ? JSON.stringify(res) : res); }; var failure = function(err){ console.log(err.toString()); }; WL.JSONStore.init(collections).then(success, failure); </code></pre> <p>We get the same output:</p> <pre><code>{"src":"initCollection","err":-12,"msg":"INVALID_SEARCH_FIELD_TYPES","col":"people","usr":"jsonstore","doc":{},"res":{}} </code></pre> <p>Mix them, pick one or use the deprecated callbacks. All JSONStore examples in the documentation should work as expected. Please update your question with real citations from the documentations and a real example showing that the documentation is wrong and I will update my answer. If it turns out something <em>is</em> wrong, I will do everything possible to fix it.</p>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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