worklight 5.0.6 JsonRestStore,使用Promises [英] worklight 5.0.6 JsonRestStore, use of Promises

查看:152
本文介绍了worklight 5.0.6 JsonRestStore,使用Promises的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这最初是一个关于Worklight文档的问题,我发现我真正的问题是关于JQuery的Deferred / Promise API和promise.then()函数。

This was originally a question about the Worklight documentation, it transpires that my real question is about JQuery's Deferred/Promise API and the promise.then() function.

上下文:

5.0.6 JsonRestStore API 提供了使用新Promise功能的示例,提供了两种可能的配方。

The documentation for the 5.0.6 JsonRestStore API gives examples of using the new Promise capability offering two possible formulations.

someFunction.then( 
             successCallback,
             errorCallback,
             optionalProgressCallback);

 someFunction().then(successCallback).fail(errorCallback)

这两种方法似乎,正如评论和答案所述,实际上是一样的。

These two approaches seem, as comments and answers state, effectively the same.

我的谜题是 JQuery文档声明then()返回新承诺。

My puzzle is that the JQuery documentation states that then() returns a "new promise".

因此在第二种情况下我们编码:

hence in the second case we are coding:

  var p1 = someFunction();
  var p2 = p1.then(successCallback);
  p2.fail(errorCallback);

我也看到人们建立了这样的行动链:

I've also seen folks set up a "chain" of actions like this:

  someFunction().then(action2).then(action3).then(action4);

设置一系列异步操作。所以我的问题变成了上面例子中承诺p1和p2之间的关系。这与链式创意有什么关系?

setting up a chain of asynchronous actions. 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?

---编辑引用答案---

--- edited to reference the answer ---

感谢cnandreu:关键是错误沿着promise链传播,直到找到错误处理程序。答案是在这里很好地解释了

Thanks to cnandreu: the key point is that "errors are propagated down a promise chain until an error handler is found." The answer is explained nicely here.

推荐答案

var p1 = someFunction();
var p2 = p1.then(successCallback);
p2.fail(errorCallback);




所以我的问题变成了承诺之间的关系$ b $上例中的b p1和p2。这与
链的想法有什么关系?

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?

Promises是一种处理异步代码的方法,这里是一个快速视频文章解释了它们。 someFunction()将在完成任务时执行异步任务(即获取已解决)它执行 successCallback ,如果失败(即拒绝)它执行 errorCallback 。关系是 p2 是一个只有在 p1 获得拒绝。想想 p1.then(胜利,失败) p1.then(win).fail(失败)为两个写同样的事情。

Promises are a way of handling asynchronous code, here's a quick video and article that explains them. someFunction() is going to do an async task, when it finishes it (i.e. gets resolved) it executes the successCallback and if that fails (ie. is rejected) it executes the errorCallback. The relationship is that p2 is a promise that will get executed only when p1 gets rejected. Think of p1.then(win, fail) and p1.then(win).fail(fail) as two ways of writing the same thing.

原来的问题可能是指以下内容:

The original question was probably referring to the following:


在调用JSONStore异步操作
(查找,添加,删除等)后返回一个promise对象。 [...] [JSONStore.add,JSONStore.find等]失败回调,
作为.then的第二个参数传递,或者.fail的第一个参数
返回错误对象,来自

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, [...]

/v5r0m6/index.jsp?topic=%2Fcom.ibm.worklight.help.doc%2Fapiref%2Fr_class_wl_jsonstore.html\"rel =nofollow> JSONStore文档。该文档特定于JSONStore API,我们将读者引用到jQuery文档以了解promises的工作原理(请参阅jQuery的API文档以获取更多详细信息。)。

from the JSONStore Documentation. The documentation is specific to the JSONStore API, we refer readers to the jQuery documentation to learn about how promises work in general (see jQuery's API documentation for more details.).

以下是一个例子:

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());
});

输出为:

{"src":"initCollection","err":-12,"msg":"INVALID_SEARCH_FIELD_TYPES","col":"people","usr":"jsonstore","doc":{},"res":{}}

如果我们切换到 .then(成功,失败)

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);

我们得到相同的输出:

{"src":"initCollection","err":-12,"msg":"INVALID_SEARCH_FIELD_TYPES","col":"people","usr":"jsonstore","doc":{},"res":{}}

混合它们,选择一个或使用弃用的回调。文档中的所有JSONStore示例都应按预期工作。请通过文档中的真实引用更新您的问题,并提供一个显示文档错误的真实示例,我将更新我的答案。如果事实证明 错误,我会尽一切可能来解决它​​。

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 is wrong, I will do everything possible to fix it.

这篇关于worklight 5.0.6 JsonRestStore,使用Promises的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆