I want to take a chrome bookmark and find its parents, all the way up to the parent bookmark folder.
The function below works for getting the first parent of the given bookmark.
function getParent(bookmark) {
var promise = new Promise(function(resolve, reject) {
chrome.bookmarks.get(bookmark.parentId, function(nodes) {
resolve(nodes[0]);
});
});
return promise;
}
I'm having trouble getting all of the bookmarks parents. The function below doesn't work but it should show what I'm looking for.
function getParents(bookmark, parents) {
var parents = parents || [];
var promise;
getParent(bookmark).then(function(parent) {
if (parent.parentId == '0') {
parents.push(parent);
promise = Promise.resolve(parents);
} else {
parents.push(parent);
getParents(parent, parents);
}
});
return promise;
}
I guess my real question is: how do you get promises to work in recursive asynchronous functions?
Or if you have a better way of doing this, that works too.
Simply return the promise returned by .then
and either return the final value from the .then
callback, or another promise:
function getParents(bookmark, parents) {
var parents = parents || [];
return getParent(bookmark).then(function(parent) {
parents.push(parent);
return parent.parentId == '0' ? parents : getParents(parent, parents);
});
}
See http://www.html5rocks.com/en/tutorials/es6/promises/#toc-chaining
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.