带有ES6 Promise的递归树遍历 [英] Recursive Tree Walk with ES6 Promise
问题描述
我正在寻找未知深度的对象树,并通过ES6 Promise返回给定的节点. (我意识到这里使用了lodash,显然没有必要).我的树可以正常运行,但是我不清楚正确的方法来确保将顶级范围变量promise
传递到递归函数调用中,以便在调用.resolve( data )
时可用.现在,它尝试在成功的查找上执行,但是由于递归函数已被新的promise覆盖,并且未能使链条冒泡,因此无法解析promise:
I'm looking to walk an object tree of unknown depth and return a given node via an ES6 promise. (used lodash here, obviously not necessary, I realize). I've got the tree walking working fine but I'm a bit unclear the proper method to ensure that the top-level scope variable promise
is passed into the recursive function calls so that it's available when calling .resolve( data )
. Right now it attempts to execute on a successful find but fails to resolve the promise since the recursive function has overwritten with a new promise and it fails to bubble up the chain:
deepFindReturn (object, searchKey, searchVal, cb) {
if ( !cb ) cb = _.noop;
for(let x in object){
if (object.hasOwnProperty(x)) {
if ( object[searchKey] === searchVal ) {
return cb( object );
}
if ( _.isArray( object[x] ) && !this.found ){
object[x].forEach( (item) => {
this.deepFindReturn(item, searchKey, searchVal, cb);
});
} else if ( typeof object[x] === typeof {} && !this.found ) {
this.deepFindReturn(object[x], searchKey, searchVal, cb);
}
}
}
}
deepFindReturn(data, 'uuid', 'f8d1ffed-9b51-4982-97b7-60f8e074eda4')
这是我要走的树,以防万一:
here's the tree I'm walking, in case it helps:
data = {
"name":"root",
"created":"2015-04-07T20:36:29.711Z",
"createdBy":"admin",
"uuid":"9731cedc-8ed7-4367-b95d-30898c7913a1",
"primaryType":"folder",
"path":"/root",
"itemCount":7,
"items":[
{
"name":"219760_964977275754_1803638_o.jpg",
"baseVersion":"afe1994e-d9a8-47fd-a7db-dcf0e085fc05",
"created":"2015-06-01T13:30:16.490Z",
"lastModified":"2015-06-01T13:30:16.490Z",
"isCheckedOut":true,
"createdBy":"admin",
"versionHistory":"152eb76a-e0ac-446d-a7e8-47b5e5c821ed",
"primaryType":"file",
"lastModifiedBy":"admin",
"uuid":"25cc6435-6432-47f3-8dc3-f94f2788f2ef",
"parent":"9731cedc-8ed7-4367-b95d-30898c7913a1",
"mimeType":"image/jpeg",
"size":116285
},
{
"name":"Child1",
"created":"2015-04-07T21:03:41.729Z",
"createdBy":"admin",
"primaryType":"folder",
"uuid":"f8d1ffed-9b51-4982-97b7-60f8e074eda4",
"parent":"9731cedc-8ed7-4367-b95d-30898c7913a1",
"itemCount":36
},
{
"name":"Child2",
"created":"2015-04-07T21:14:47.950Z",
"createdBy":"admin",
"uuid":"8f1246ff-5053-411a-88de-c465027b998d",
"primaryType":"folder",
"parent":"9731cedc-8ed7-4367-b95d-30898c7913a1",
"itemCount":3
},
{
"name":"Child3",
"created":"2015-05-01T00:46:36.973Z",
"createdBy":"admin",
"uuid":"54f897a4-ac16-4585-83cb-d0e67ca73a74",
"primaryType":"folder",
"parent":"9731cedc-8ed7-4367-b95d-30898c7913a1",
"itemCount":1
},
{
"name":"Child4",
"created":"2015-05-26T18:18:33.159Z",
"createdBy":"admin",
"primaryType":"folder",
"uuid":"ad1344a9-08b7-44bb-b47d-0efb99c59ac3",
"parent":"9731cedc-8ed7-4367-b95d-30898c7913a1",
"itemCount":0
},
{
"name":"Child5",
"created":"2015-06-07T03:57:20.494Z",
"createdBy":"admin",
"primaryType":"folder",
"uuid":"2b46d7e4-b50e-4eec-b97a-c46b2016926c",
"parent":"9731cedc-8ed7-4367-b95d-30898c7913a1",
"itemCount":0
},
{
"name":"content.jpg",
"baseVersion":"620c8448-3e51-4a27-b630-60c1272c19da",
"created":"2015-06-03T15:09:25.192Z",
"lastModified":"2015-06-03T15:09:25.193Z",
"isCheckedOut":true,
"createdBy":"admin",
"versionHistory":"871afa2a-5a2c-4762-bc26-a69022234850",
"primaryType":"file",
"lastModifiedBy":"admin",
"uuid":"c8c63420-b525-4b36-bce3-a7c4cc55c07a",
"parent":"9731cedc-8ed7-4367-b95d-30898c7913a1",
"mimeType":"image/jpeg",
"size":30711
}
]
}
推荐答案
我将大部分逻辑移到另一个函数中,并将该函数包装在Promise
中.我不确定为树的每个分支递归创建promise是否有任何好处.该功能是非阻塞的,对逻辑的更改很小.
I'd move the bulk of the logic into another function and wrap that function in a Promise
. I'm not sure the is any benefit to recursively creating promises for each branch of the tree. The function is non-blocking, with only minimal changes to logic.
function deepFindReturn (object, searchKey, searchVal) {
function doer(object, searchKey, searchVal) {
if ( object[searchKey] === searchVal ) {
return object;
}
for(let x in object) {
let val = object[x];
if (typeof val === typeof searchVal) {
continue;
}
if ( Array.isArray( val ) ) {
for (let item of val) {
let o = doer(item, searchKey, searchVal);
if (o) {
return o;
}
}
}
else if ( typeof val === 'object' ) {
let o = doer(val, searchKey, searchVal);
if (o) {
return o;
}
}
}
};
return new Promise(function(resolve) {
resolve(doer(object, searchKey, searchVal));
});
}
deepFindReturn(data, 'uuid', 'f8d1ffed-9b51-4982-97b7-60f8e074eda4')
.then((o) => { console.log(o); });
但是,如果练习的重点是尝试递归异步代码,那么我来看看 Async 而不是承诺.在递归情况下更多可用.
If however, the point of the exercise was to experiment with recursive asynchronous code, I'd have a look at Async instead of promises. Much more usable in recursive situations.
我认为您可以使用promises的唯一方法是为每次迭代生成Promise
并将它们添加到新数组中.您会将新数组传递给 Promise.race .像这样:
The only way I think you can do it with promises, would be to generate a Promise
for each iteration and add them to a new array. You'd pass that new array to Promise.race. Something like:
Promise.race(val.map(function(item) {
return new Promise(function(resolve) {
// Determine result
resolve(result);
});
}))
.then((result) => { /* Deal with result */ });
这篇关于带有ES6 Promise的递归树遍历的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!