Javascript 递归异步调用中的回调处理
我使用Jaydata作为HTML5 indexedDB的API。我在indexedDB中有一个表,需要在其中递归查询。我需要一个回调,当整个过程完成。下面是递归函数。当一切完成后,我需要回电话Javascript 递归异步调用中的回调处理,javascript,html,recursion,callback,jaydata,Javascript,Html,Recursion,Callback,Jaydata,我使用Jaydata作为HTML5 indexedDB的API。我在indexedDB中有一个表,需要在其中递归查询。我需要一个回调,当整个过程完成。下面是递归函数。当一切完成后,我需要回电话 function getData(idValue) { myDB.MySplDB .filter( function(val) { return val.ParentId == this.parentId; }, {parentId: idv
function getData(idValue) {
myDB.MySplDB
.filter( function(val) {
return val.ParentId == this.parentId;
}, {parentId: idvalue})
.toArray( function(vals) {
if(vals.length < 1) {
// some operation to store the value
} else {
for (var j=0;j<vals.length;j++) {
getData(vals[j].Id);
}
}
});
}
函数getData(idValue){
myDB.MySplDB
.过滤器(功能(val){
return val.ParentId==this.ParentId;
},{parentId:idvalue})
.toArray(功能(VAL){
如果(vals.length<1){
//一些用于存储值的操作
}否则{
对于(var j=0;j这个伪代码在您的情况下有意义吗
var helper = function (accu) {
// Take an id from the accumulator
// query the db for new ids, and in the db query callback :
// If there is child, do "some operation to store the value" (I'm not sure what you're trying to do here
// Else add the child to the accumulator
// if accu is empty, call the callback, it means you reached the end
getData()将使用包含第一个id的累加器调用此帮助程序,您的最终回调此伪代码在您的情况下有意义吗
var helper = function (accu) {
// Take an id from the accumulator
// query the db for new ids, and in the db query callback :
// If there is child, do "some operation to store the value" (I'm not sure what you're trying to do here
// Else add the child to the accumulator
// if accu is empty, call the callback, it means you reached the end
getData()将使用包含第一个id的累加器调用此帮助程序,以及您的最终回调(免责声明:我为JayData工作)
要等待整个过程的完成,您需要使用承诺。您始终必须返回承诺。在循环中,返回超级承诺会变得很棘手。因此代码应该如下所示:
function getData(idValue) {
return myDB.MySplDB
.filter( function(val) {
return val.ParentId == this.parentId;
}, {parentId: idvalue})
.toArray( function(vals) {
if(vals.length < 1) {
// some operation to store the value
// important: return a promise from here, like:
return myDB.saveChanges();
} else {
var promises = [];
for (var j=0;j<vals.length;j++) {
promises.push(getData(vals[j].Id));
}
return $.when.apply(this, promises);
}
});
}
getData(1)
.then(function() {
// this will run after everything is finished
});
函数getData(idValue){
返回myDB.MySplDB
.过滤器(功能(val){
return val.ParentId==this.ParentId;
},{parentId:idvalue})
.toArray(功能(VAL){
如果(vals.length<1){
//一些用于存储值的操作
//重要提示:从此处返回承诺,如:
返回myDB.saveChanges();
}否则{
var承诺=[];
对于(var j=0;j(免责声明:我为JayData工作)
要等待整个过程的完成,您需要使用承诺。您始终必须返回承诺。在循环中,返回超级承诺会变得很棘手。因此代码应该如下所示:
function getData(idValue) {
return myDB.MySplDB
.filter( function(val) {
return val.ParentId == this.parentId;
}, {parentId: idvalue})
.toArray( function(vals) {
if(vals.length < 1) {
// some operation to store the value
// important: return a promise from here, like:
return myDB.saveChanges();
} else {
var promises = [];
for (var j=0;j<vals.length;j++) {
promises.push(getData(vals[j].Id));
}
return $.when.apply(this, promises);
}
});
}
getData(1)
.then(function() {
// this will run after everything is finished
});
函数getData(idValue){
返回myDB.MySplDB
.过滤器(功能(val){
return val.ParentId==this.ParentId;
},{parentId:idvalue})
.toArray(功能(VAL){
如果(vals.length<1){
//一些用于存储值的操作
//重要提示:从此处返回承诺,如:
返回myDB.saveChanges();
}否则{
var承诺=[];
对于(var j=0;jIn)节
//存储值的某些操作
,我没有将值存储在数据库中。我将这些值存储在一个数组中。目的是获取所有没有子元素的元素,即对象的Id不是任何其他对象的parentId。我还必须返回什么来代替返回myDB.saveChanges();
如果我没有将值存储在DB中?在这种情况下,您可以返回任何内容,如return true;谢谢。请您解释一下var promissions=[];部分,好吗(var j=0;jok,我从不使用回调函数,但我希望尽可能少地更改您的代码,因此我保留了您的回调函数,但回调无法返回承诺。另外,免费indexeddb提供程序不喜欢以递归并行方式调用,因此我重写了该部分以构建承诺链。您可以在以下位置找到它:在节//存储值的某些操作
,我不是在数据库中存储值。我是在数组中存储这些值。目的是获取所有没有子元素的元素,即对象的Id不是任何其他对象的父Id。我还必须返回什么来代替返回myDB.saveChanges();
如果我没有将值存储在DB中?在这种情况下,您可以返回任何内容,如return true;谢谢。请您解释一下var promissions=[];部分,好吗(var j=0;jok,我从不使用回调函数,但我希望尽可能少地更改您的代码,因此我保留了您的回调函数,但回调无法返回承诺。此外,免费indexeddb提供程序不喜欢以递归并行方式调用,因此我重写了该部分以构建承诺链。您可以在此处找到它: