, searchNode Promise.
:
, , , , promises. .
null , , , promises, .
, promises, , , , ( ), , ... . , , .
:
function searchNode(searchValue, path){
return tree.getChildren(path).then(function(children){
for(let i = 0; i<children.length; i++){
if(children[i] == searchValue){
return path;
}
}
return (function loop(i) {
if (i >= children.length) {
return Promise.reject("not found");
} else {
return searchNode(searchValue, path+"/"+children[i])
.catch(loop.bind(null, i+1));
}
})(0);
}
"use strict";
const tree = {
root: {
'2': {
'4': {
'1': {}
},
'7': {}
},
'0': {}
},
getChildren: function(path) {
return new Promise(function (resolve) {
const node = path.split('/').reduce(function (parent, node) {
return node === '' || !parent ? parent : parent[node];
}, tree.root);
resolve(Object.keys(node));
});
}
};
function searchNode(searchValue, path){
return tree.getChildren(path).then(function(children){
for(let i = 0; i<children.length; i++){
if(children[i] == searchValue){
return path;
}
}
return (function loop(i) {
if (i >= children.length) {
return Promise.reject("not found");
} else {
return searchNode(searchValue, path+"/"+children[i])
.catch(loop.bind(null, i+1));
}
})(0);
})
}
searchNode('1', '').then(function (path) {
console.log(path);
}, function (reason) {
console.log(reason);
});
Hide result. node, , sibling node. , tree.getChildren , :
, node 1000 , node. , . , - , , " " () , , . , , .
, searchNode then , , .
- Promise.not Promise.some -, Promise.race Promise.all. Q & A, " ES6 ?" :
Promise.not = promise => promise.then(x => {throw x}, e => e);
Promise.some = iterable => Promise.not(Promise.all(iterable.map(Promise.not)));
, bluebird Promise.any.
, , . , . , , .
Promise.some :
function searchNode(searchValue, path){
let resolved = false;
return (function searchNodeSub(path) {
return tree.getChildren(path).then(function(children){
return resolved ? true
: children.some(child => child == searchValue) ? path
: Promise.some(children.map(child => searchNodeSub(path+"/"+child)));
})
})(path).then( path => (resolved = true, path) );
}
children.some Promise.some.
"use strict";
const tree = {
root: {
'2': {
'4': {
'1': {}
},
'7': {}
},
'0': {}
},
getChildren: function(path) {
return new Promise(function (resolve) {
let node = path.split('/').reduce(function (parent, node) {
return node === '' || !parent ? parent : parent[node];
}, tree.root);
resolve(Object.keys(node));
});
}
};
Promise.not = promise => promise.then(x => {throw x}, e => e);
Promise.some = iterable => Promise.not(Promise.all(iterable.map(Promise.not)));
function searchNode(searchValue, path){
let resolved = false;
return (function searchNodeSub(path) {
return tree.getChildren(path).then(function(children){
return resolved ? true
: children.some(child => child == searchValue) ? path
: Promise.some(children.map(child => searchNodeSub(path+"/"+child)));
})
})(path).then( path => (resolved = true, path) );
}
searchNode('1', '').then(function (path) {
console.log(path);
}, function (reason) {
console.log('Not found');
});
Hide result