Recursive function Chrome Bookmarks loop & appending











up vote
1
down vote

favorite












I'm trying to loop trough the chrome bookmarks and get all folders by using recursive function. I'm able to log all folders correctly so _.each and if folder works fine but struggling to correctly append <ul><li>



The object chrome.bookmarks.getSubTree ASYNC:



http://www.mocky.io/v2/5be950062e00006700f144d6






[
{
"children": [
{
"children": [
{
"dateAdded": 1509653754344,
"id": "459",
"index": 18,
"parentId": "1",
"title": "Test 1",
"url": "https://www.test1.net/"
},
{
"dateAdded": 1509653754369,
"id": "460",
"index": 19,
"parentId": "1",
"title": "Test 2",
"url": "https://www.test2.net/"
}
],
"dateAdded": 1529656829217,
"dateGroupModified": 1538037498559,
"id": "1",
"index": 0,
"parentId": "0",
"title": "Bookmarks Bar"
},
{
"children": [
{
"children": [
{
"children": [
{
"children": ,
"dateAdded": 1542016268115,
"dateGroupModified": 1542016268115,
"id": "496",
"index": 0,
"parentId": "492",
"title": "F40"
},
{
"children": ,
"dateAdded": 1542016288842,
"dateGroupModified": 1542016288842,
"id": "497",
"index": 1,
"parentId": "492",
"title": "Testarossa"
}
],
"dateAdded": 1542016224744,
"dateGroupModified": 1542016288843,
"id": "492",
"index": 0,
"parentId": "489",
"title": "Ferrari"
},
{
"children": ,
"dateAdded": 1542016232765,
"dateGroupModified": 1542016232765,
"id": "493",
"index": 1,
"parentId": "489",
"title": "Toyota"
},
{
"children": ,
"dateAdded": 1542016245690,
"dateGroupModified": 1542016245690,
"id": "494",
"index": 2,
"parentId": "489",
"title": "BMW"
},
{
"children": ,
"dateAdded": 1542016253590,
"dateGroupModified": 1542016253590,
"id": "495",
"index": 3,
"parentId": "489",
"title": "Audi"
}
],
"dateAdded": 1542016199154,
"dateGroupModified": 1542016253590,
"id": "489",
"index": 0,
"parentId": "2",
"title": "Cars"
},
{
"children": [
{
"children": ,
"dateAdded": 1542016326727,
"dateGroupModified": 1542016326727,
"id": "498",
"index": 0,
"parentId": "490",
"title": "Boeing"
},
{
"children": ,
"dateAdded": 1542016335148,
"dateGroupModified": 1542016335148,
"id": "499",
"index": 1,
"parentId": "490",
"title": "Airbus"
}
],
"dateAdded": 1542016208019,
"dateGroupModified": 1542016335149,
"id": "490",
"index": 1,
"parentId": "2",
"title": "Planes"
},
{
"children": ,
"dateAdded": 1542016213955,
"dateGroupModified": 1542016213955,
"id": "491",
"index": 2,
"parentId": "2",
"title": "Ships"
}
],
"dateAdded": 1529656829217,
"dateGroupModified": 1542016213955,
"id": "2",
"index": 1,
"parentId": "0",
"title": "Other Bookmarks"
}
],
"dateAdded": 1541543894421,
"id": "0",
"title": ""
}
]





Here is my function, at the beginning getFolders gets the very top level ID eg. 2:



const getFolders = (bmkNode) => {
const props = {
menuContainerId: $('#bookmarks')
}

const getTree = (element, bmkNode) => {
const ul = $('<ul data-node="' + bmkNode + '"></ul>')
let li

chrome.bookmarks.getSubTree(bmkNode, (folder) => {
_.map(folder[0].children, (item) => {

if (item.url === undefined || item.url === null) {

li = $('<li data-node="' + item.id + '">' + item.title + '</li>')
ul = $('<ul data-node="' + item.parentId + '"></ul>') //v1
ul.append(li)

if (_.size(item.children) > 0) {

getTree(ul, item.id)

}

element.append(ul)

}

})
})

}

getTree(props.menuContainerId, bmkNode)

}

getFolders('0')


My current output:



<div id="bookmarks">
<ul data-node="0">
<li data-node="1">Bookmarks Bar</li>
</ul>
<ul data-node="0">
<li data-node="2">Other Bookmarks</li>
<ul data-node="2">
<li data-node="489">Cars</li>
<ul data-node="489">
<li data-node="492">Ferrari</li>
<ul data-node="492">
<li data-node="496">F40</li>
</ul>
<ul data-node="492">
<li data-node="497">Testarossa</li>
</ul>
</ul>
<ul data-node="489">
<li data-node="493">Toyota</li>
</ul>
<ul data-node="489">
<li data-node="494">BMW</li>
</ul>
<ul data-node="489">
<li data-node="495">Audi</li>
</ul>
</ul>
<ul data-node="2">
<li data-node="490">Planes</li>
<ul data-node="490">
<li data-node="498">Boeing</li>
</ul>
<ul data-node="490">
<li data-node="499">Airbus</li>
</ul>
</ul>
<ul data-node="2">
<li data-node="491">Ships</li>
</ul>
</ul>
</div>


My desire output:



 <div id="bookmarks">
<ul>
<li>Bookmarks Bar</li>
<li>Other Bookmarks</li>
<ul>
<li>Cars
<li>Ferrari
<ul>
<li>F40</li>
<li>Testarossa</li>
</ul>
</li>
<li>Toyota</li>
<li>BMW</li>
<li>Audi</li>
</li>
<li>Planes</li>
<li>Ships</li>
<ul>

</ul>
<ul>
<li>Boeing</li>
<li>Airbus</li>
</ul>
</ul>
</ul>
</div>









share|improve this question
























  • Do not use _.each. It does not work with asynchronous code. _.map them to an array of promises and use Promise.all.
    – Bergi
    Nov 12 at 8:24










  • Promisify only the chrome.bookmarks.getSubTree(bmkNode) function, put nothing else into the new Promise constructor. Then use .then() chaining to work with the result values
    – Bergi
    Nov 12 at 8:26










  • @Bergi I thought we are using _.map when we are expecting to return something? There is no return here..? Could you suggest the function changes below? Thanks
    – Ben
    Nov 12 at 9:05












  • Try to find something useful that could be returned (hint: don't use a global folderItem variable). And since the recursive getTrees call is asynchronous, you will need to return a promise for this thing. Then you will have an array of promises in the end, which is something that Promise.all can work with.
    – Bergi
    Nov 12 at 9:12










  • @Bergi I've edited my example so there is no need for promise also added actual object. Tell me what you think? Thanks
    – Ben
    Nov 12 at 10:47















up vote
1
down vote

favorite












I'm trying to loop trough the chrome bookmarks and get all folders by using recursive function. I'm able to log all folders correctly so _.each and if folder works fine but struggling to correctly append <ul><li>



The object chrome.bookmarks.getSubTree ASYNC:



http://www.mocky.io/v2/5be950062e00006700f144d6






[
{
"children": [
{
"children": [
{
"dateAdded": 1509653754344,
"id": "459",
"index": 18,
"parentId": "1",
"title": "Test 1",
"url": "https://www.test1.net/"
},
{
"dateAdded": 1509653754369,
"id": "460",
"index": 19,
"parentId": "1",
"title": "Test 2",
"url": "https://www.test2.net/"
}
],
"dateAdded": 1529656829217,
"dateGroupModified": 1538037498559,
"id": "1",
"index": 0,
"parentId": "0",
"title": "Bookmarks Bar"
},
{
"children": [
{
"children": [
{
"children": [
{
"children": ,
"dateAdded": 1542016268115,
"dateGroupModified": 1542016268115,
"id": "496",
"index": 0,
"parentId": "492",
"title": "F40"
},
{
"children": ,
"dateAdded": 1542016288842,
"dateGroupModified": 1542016288842,
"id": "497",
"index": 1,
"parentId": "492",
"title": "Testarossa"
}
],
"dateAdded": 1542016224744,
"dateGroupModified": 1542016288843,
"id": "492",
"index": 0,
"parentId": "489",
"title": "Ferrari"
},
{
"children": ,
"dateAdded": 1542016232765,
"dateGroupModified": 1542016232765,
"id": "493",
"index": 1,
"parentId": "489",
"title": "Toyota"
},
{
"children": ,
"dateAdded": 1542016245690,
"dateGroupModified": 1542016245690,
"id": "494",
"index": 2,
"parentId": "489",
"title": "BMW"
},
{
"children": ,
"dateAdded": 1542016253590,
"dateGroupModified": 1542016253590,
"id": "495",
"index": 3,
"parentId": "489",
"title": "Audi"
}
],
"dateAdded": 1542016199154,
"dateGroupModified": 1542016253590,
"id": "489",
"index": 0,
"parentId": "2",
"title": "Cars"
},
{
"children": [
{
"children": ,
"dateAdded": 1542016326727,
"dateGroupModified": 1542016326727,
"id": "498",
"index": 0,
"parentId": "490",
"title": "Boeing"
},
{
"children": ,
"dateAdded": 1542016335148,
"dateGroupModified": 1542016335148,
"id": "499",
"index": 1,
"parentId": "490",
"title": "Airbus"
}
],
"dateAdded": 1542016208019,
"dateGroupModified": 1542016335149,
"id": "490",
"index": 1,
"parentId": "2",
"title": "Planes"
},
{
"children": ,
"dateAdded": 1542016213955,
"dateGroupModified": 1542016213955,
"id": "491",
"index": 2,
"parentId": "2",
"title": "Ships"
}
],
"dateAdded": 1529656829217,
"dateGroupModified": 1542016213955,
"id": "2",
"index": 1,
"parentId": "0",
"title": "Other Bookmarks"
}
],
"dateAdded": 1541543894421,
"id": "0",
"title": ""
}
]





Here is my function, at the beginning getFolders gets the very top level ID eg. 2:



const getFolders = (bmkNode) => {
const props = {
menuContainerId: $('#bookmarks')
}

const getTree = (element, bmkNode) => {
const ul = $('<ul data-node="' + bmkNode + '"></ul>')
let li

chrome.bookmarks.getSubTree(bmkNode, (folder) => {
_.map(folder[0].children, (item) => {

if (item.url === undefined || item.url === null) {

li = $('<li data-node="' + item.id + '">' + item.title + '</li>')
ul = $('<ul data-node="' + item.parentId + '"></ul>') //v1
ul.append(li)

if (_.size(item.children) > 0) {

getTree(ul, item.id)

}

element.append(ul)

}

})
})

}

getTree(props.menuContainerId, bmkNode)

}

getFolders('0')


My current output:



<div id="bookmarks">
<ul data-node="0">
<li data-node="1">Bookmarks Bar</li>
</ul>
<ul data-node="0">
<li data-node="2">Other Bookmarks</li>
<ul data-node="2">
<li data-node="489">Cars</li>
<ul data-node="489">
<li data-node="492">Ferrari</li>
<ul data-node="492">
<li data-node="496">F40</li>
</ul>
<ul data-node="492">
<li data-node="497">Testarossa</li>
</ul>
</ul>
<ul data-node="489">
<li data-node="493">Toyota</li>
</ul>
<ul data-node="489">
<li data-node="494">BMW</li>
</ul>
<ul data-node="489">
<li data-node="495">Audi</li>
</ul>
</ul>
<ul data-node="2">
<li data-node="490">Planes</li>
<ul data-node="490">
<li data-node="498">Boeing</li>
</ul>
<ul data-node="490">
<li data-node="499">Airbus</li>
</ul>
</ul>
<ul data-node="2">
<li data-node="491">Ships</li>
</ul>
</ul>
</div>


My desire output:



 <div id="bookmarks">
<ul>
<li>Bookmarks Bar</li>
<li>Other Bookmarks</li>
<ul>
<li>Cars
<li>Ferrari
<ul>
<li>F40</li>
<li>Testarossa</li>
</ul>
</li>
<li>Toyota</li>
<li>BMW</li>
<li>Audi</li>
</li>
<li>Planes</li>
<li>Ships</li>
<ul>

</ul>
<ul>
<li>Boeing</li>
<li>Airbus</li>
</ul>
</ul>
</ul>
</div>









share|improve this question
























  • Do not use _.each. It does not work with asynchronous code. _.map them to an array of promises and use Promise.all.
    – Bergi
    Nov 12 at 8:24










  • Promisify only the chrome.bookmarks.getSubTree(bmkNode) function, put nothing else into the new Promise constructor. Then use .then() chaining to work with the result values
    – Bergi
    Nov 12 at 8:26










  • @Bergi I thought we are using _.map when we are expecting to return something? There is no return here..? Could you suggest the function changes below? Thanks
    – Ben
    Nov 12 at 9:05












  • Try to find something useful that could be returned (hint: don't use a global folderItem variable). And since the recursive getTrees call is asynchronous, you will need to return a promise for this thing. Then you will have an array of promises in the end, which is something that Promise.all can work with.
    – Bergi
    Nov 12 at 9:12










  • @Bergi I've edited my example so there is no need for promise also added actual object. Tell me what you think? Thanks
    – Ben
    Nov 12 at 10:47













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I'm trying to loop trough the chrome bookmarks and get all folders by using recursive function. I'm able to log all folders correctly so _.each and if folder works fine but struggling to correctly append <ul><li>



The object chrome.bookmarks.getSubTree ASYNC:



http://www.mocky.io/v2/5be950062e00006700f144d6






[
{
"children": [
{
"children": [
{
"dateAdded": 1509653754344,
"id": "459",
"index": 18,
"parentId": "1",
"title": "Test 1",
"url": "https://www.test1.net/"
},
{
"dateAdded": 1509653754369,
"id": "460",
"index": 19,
"parentId": "1",
"title": "Test 2",
"url": "https://www.test2.net/"
}
],
"dateAdded": 1529656829217,
"dateGroupModified": 1538037498559,
"id": "1",
"index": 0,
"parentId": "0",
"title": "Bookmarks Bar"
},
{
"children": [
{
"children": [
{
"children": [
{
"children": ,
"dateAdded": 1542016268115,
"dateGroupModified": 1542016268115,
"id": "496",
"index": 0,
"parentId": "492",
"title": "F40"
},
{
"children": ,
"dateAdded": 1542016288842,
"dateGroupModified": 1542016288842,
"id": "497",
"index": 1,
"parentId": "492",
"title": "Testarossa"
}
],
"dateAdded": 1542016224744,
"dateGroupModified": 1542016288843,
"id": "492",
"index": 0,
"parentId": "489",
"title": "Ferrari"
},
{
"children": ,
"dateAdded": 1542016232765,
"dateGroupModified": 1542016232765,
"id": "493",
"index": 1,
"parentId": "489",
"title": "Toyota"
},
{
"children": ,
"dateAdded": 1542016245690,
"dateGroupModified": 1542016245690,
"id": "494",
"index": 2,
"parentId": "489",
"title": "BMW"
},
{
"children": ,
"dateAdded": 1542016253590,
"dateGroupModified": 1542016253590,
"id": "495",
"index": 3,
"parentId": "489",
"title": "Audi"
}
],
"dateAdded": 1542016199154,
"dateGroupModified": 1542016253590,
"id": "489",
"index": 0,
"parentId": "2",
"title": "Cars"
},
{
"children": [
{
"children": ,
"dateAdded": 1542016326727,
"dateGroupModified": 1542016326727,
"id": "498",
"index": 0,
"parentId": "490",
"title": "Boeing"
},
{
"children": ,
"dateAdded": 1542016335148,
"dateGroupModified": 1542016335148,
"id": "499",
"index": 1,
"parentId": "490",
"title": "Airbus"
}
],
"dateAdded": 1542016208019,
"dateGroupModified": 1542016335149,
"id": "490",
"index": 1,
"parentId": "2",
"title": "Planes"
},
{
"children": ,
"dateAdded": 1542016213955,
"dateGroupModified": 1542016213955,
"id": "491",
"index": 2,
"parentId": "2",
"title": "Ships"
}
],
"dateAdded": 1529656829217,
"dateGroupModified": 1542016213955,
"id": "2",
"index": 1,
"parentId": "0",
"title": "Other Bookmarks"
}
],
"dateAdded": 1541543894421,
"id": "0",
"title": ""
}
]





Here is my function, at the beginning getFolders gets the very top level ID eg. 2:



const getFolders = (bmkNode) => {
const props = {
menuContainerId: $('#bookmarks')
}

const getTree = (element, bmkNode) => {
const ul = $('<ul data-node="' + bmkNode + '"></ul>')
let li

chrome.bookmarks.getSubTree(bmkNode, (folder) => {
_.map(folder[0].children, (item) => {

if (item.url === undefined || item.url === null) {

li = $('<li data-node="' + item.id + '">' + item.title + '</li>')
ul = $('<ul data-node="' + item.parentId + '"></ul>') //v1
ul.append(li)

if (_.size(item.children) > 0) {

getTree(ul, item.id)

}

element.append(ul)

}

})
})

}

getTree(props.menuContainerId, bmkNode)

}

getFolders('0')


My current output:



<div id="bookmarks">
<ul data-node="0">
<li data-node="1">Bookmarks Bar</li>
</ul>
<ul data-node="0">
<li data-node="2">Other Bookmarks</li>
<ul data-node="2">
<li data-node="489">Cars</li>
<ul data-node="489">
<li data-node="492">Ferrari</li>
<ul data-node="492">
<li data-node="496">F40</li>
</ul>
<ul data-node="492">
<li data-node="497">Testarossa</li>
</ul>
</ul>
<ul data-node="489">
<li data-node="493">Toyota</li>
</ul>
<ul data-node="489">
<li data-node="494">BMW</li>
</ul>
<ul data-node="489">
<li data-node="495">Audi</li>
</ul>
</ul>
<ul data-node="2">
<li data-node="490">Planes</li>
<ul data-node="490">
<li data-node="498">Boeing</li>
</ul>
<ul data-node="490">
<li data-node="499">Airbus</li>
</ul>
</ul>
<ul data-node="2">
<li data-node="491">Ships</li>
</ul>
</ul>
</div>


My desire output:



 <div id="bookmarks">
<ul>
<li>Bookmarks Bar</li>
<li>Other Bookmarks</li>
<ul>
<li>Cars
<li>Ferrari
<ul>
<li>F40</li>
<li>Testarossa</li>
</ul>
</li>
<li>Toyota</li>
<li>BMW</li>
<li>Audi</li>
</li>
<li>Planes</li>
<li>Ships</li>
<ul>

</ul>
<ul>
<li>Boeing</li>
<li>Airbus</li>
</ul>
</ul>
</ul>
</div>









share|improve this question















I'm trying to loop trough the chrome bookmarks and get all folders by using recursive function. I'm able to log all folders correctly so _.each and if folder works fine but struggling to correctly append <ul><li>



The object chrome.bookmarks.getSubTree ASYNC:



http://www.mocky.io/v2/5be950062e00006700f144d6






[
{
"children": [
{
"children": [
{
"dateAdded": 1509653754344,
"id": "459",
"index": 18,
"parentId": "1",
"title": "Test 1",
"url": "https://www.test1.net/"
},
{
"dateAdded": 1509653754369,
"id": "460",
"index": 19,
"parentId": "1",
"title": "Test 2",
"url": "https://www.test2.net/"
}
],
"dateAdded": 1529656829217,
"dateGroupModified": 1538037498559,
"id": "1",
"index": 0,
"parentId": "0",
"title": "Bookmarks Bar"
},
{
"children": [
{
"children": [
{
"children": [
{
"children": ,
"dateAdded": 1542016268115,
"dateGroupModified": 1542016268115,
"id": "496",
"index": 0,
"parentId": "492",
"title": "F40"
},
{
"children": ,
"dateAdded": 1542016288842,
"dateGroupModified": 1542016288842,
"id": "497",
"index": 1,
"parentId": "492",
"title": "Testarossa"
}
],
"dateAdded": 1542016224744,
"dateGroupModified": 1542016288843,
"id": "492",
"index": 0,
"parentId": "489",
"title": "Ferrari"
},
{
"children": ,
"dateAdded": 1542016232765,
"dateGroupModified": 1542016232765,
"id": "493",
"index": 1,
"parentId": "489",
"title": "Toyota"
},
{
"children": ,
"dateAdded": 1542016245690,
"dateGroupModified": 1542016245690,
"id": "494",
"index": 2,
"parentId": "489",
"title": "BMW"
},
{
"children": ,
"dateAdded": 1542016253590,
"dateGroupModified": 1542016253590,
"id": "495",
"index": 3,
"parentId": "489",
"title": "Audi"
}
],
"dateAdded": 1542016199154,
"dateGroupModified": 1542016253590,
"id": "489",
"index": 0,
"parentId": "2",
"title": "Cars"
},
{
"children": [
{
"children": ,
"dateAdded": 1542016326727,
"dateGroupModified": 1542016326727,
"id": "498",
"index": 0,
"parentId": "490",
"title": "Boeing"
},
{
"children": ,
"dateAdded": 1542016335148,
"dateGroupModified": 1542016335148,
"id": "499",
"index": 1,
"parentId": "490",
"title": "Airbus"
}
],
"dateAdded": 1542016208019,
"dateGroupModified": 1542016335149,
"id": "490",
"index": 1,
"parentId": "2",
"title": "Planes"
},
{
"children": ,
"dateAdded": 1542016213955,
"dateGroupModified": 1542016213955,
"id": "491",
"index": 2,
"parentId": "2",
"title": "Ships"
}
],
"dateAdded": 1529656829217,
"dateGroupModified": 1542016213955,
"id": "2",
"index": 1,
"parentId": "0",
"title": "Other Bookmarks"
}
],
"dateAdded": 1541543894421,
"id": "0",
"title": ""
}
]





Here is my function, at the beginning getFolders gets the very top level ID eg. 2:



const getFolders = (bmkNode) => {
const props = {
menuContainerId: $('#bookmarks')
}

const getTree = (element, bmkNode) => {
const ul = $('<ul data-node="' + bmkNode + '"></ul>')
let li

chrome.bookmarks.getSubTree(bmkNode, (folder) => {
_.map(folder[0].children, (item) => {

if (item.url === undefined || item.url === null) {

li = $('<li data-node="' + item.id + '">' + item.title + '</li>')
ul = $('<ul data-node="' + item.parentId + '"></ul>') //v1
ul.append(li)

if (_.size(item.children) > 0) {

getTree(ul, item.id)

}

element.append(ul)

}

})
})

}

getTree(props.menuContainerId, bmkNode)

}

getFolders('0')


My current output:



<div id="bookmarks">
<ul data-node="0">
<li data-node="1">Bookmarks Bar</li>
</ul>
<ul data-node="0">
<li data-node="2">Other Bookmarks</li>
<ul data-node="2">
<li data-node="489">Cars</li>
<ul data-node="489">
<li data-node="492">Ferrari</li>
<ul data-node="492">
<li data-node="496">F40</li>
</ul>
<ul data-node="492">
<li data-node="497">Testarossa</li>
</ul>
</ul>
<ul data-node="489">
<li data-node="493">Toyota</li>
</ul>
<ul data-node="489">
<li data-node="494">BMW</li>
</ul>
<ul data-node="489">
<li data-node="495">Audi</li>
</ul>
</ul>
<ul data-node="2">
<li data-node="490">Planes</li>
<ul data-node="490">
<li data-node="498">Boeing</li>
</ul>
<ul data-node="490">
<li data-node="499">Airbus</li>
</ul>
</ul>
<ul data-node="2">
<li data-node="491">Ships</li>
</ul>
</ul>
</div>


My desire output:



 <div id="bookmarks">
<ul>
<li>Bookmarks Bar</li>
<li>Other Bookmarks</li>
<ul>
<li>Cars
<li>Ferrari
<ul>
<li>F40</li>
<li>Testarossa</li>
</ul>
</li>
<li>Toyota</li>
<li>BMW</li>
<li>Audi</li>
</li>
<li>Planes</li>
<li>Ships</li>
<ul>

</ul>
<ul>
<li>Boeing</li>
<li>Airbus</li>
</ul>
</ul>
</ul>
</div>





[
{
"children": [
{
"children": [
{
"dateAdded": 1509653754344,
"id": "459",
"index": 18,
"parentId": "1",
"title": "Test 1",
"url": "https://www.test1.net/"
},
{
"dateAdded": 1509653754369,
"id": "460",
"index": 19,
"parentId": "1",
"title": "Test 2",
"url": "https://www.test2.net/"
}
],
"dateAdded": 1529656829217,
"dateGroupModified": 1538037498559,
"id": "1",
"index": 0,
"parentId": "0",
"title": "Bookmarks Bar"
},
{
"children": [
{
"children": [
{
"children": [
{
"children": ,
"dateAdded": 1542016268115,
"dateGroupModified": 1542016268115,
"id": "496",
"index": 0,
"parentId": "492",
"title": "F40"
},
{
"children": ,
"dateAdded": 1542016288842,
"dateGroupModified": 1542016288842,
"id": "497",
"index": 1,
"parentId": "492",
"title": "Testarossa"
}
],
"dateAdded": 1542016224744,
"dateGroupModified": 1542016288843,
"id": "492",
"index": 0,
"parentId": "489",
"title": "Ferrari"
},
{
"children": ,
"dateAdded": 1542016232765,
"dateGroupModified": 1542016232765,
"id": "493",
"index": 1,
"parentId": "489",
"title": "Toyota"
},
{
"children": ,
"dateAdded": 1542016245690,
"dateGroupModified": 1542016245690,
"id": "494",
"index": 2,
"parentId": "489",
"title": "BMW"
},
{
"children": ,
"dateAdded": 1542016253590,
"dateGroupModified": 1542016253590,
"id": "495",
"index": 3,
"parentId": "489",
"title": "Audi"
}
],
"dateAdded": 1542016199154,
"dateGroupModified": 1542016253590,
"id": "489",
"index": 0,
"parentId": "2",
"title": "Cars"
},
{
"children": [
{
"children": ,
"dateAdded": 1542016326727,
"dateGroupModified": 1542016326727,
"id": "498",
"index": 0,
"parentId": "490",
"title": "Boeing"
},
{
"children": ,
"dateAdded": 1542016335148,
"dateGroupModified": 1542016335148,
"id": "499",
"index": 1,
"parentId": "490",
"title": "Airbus"
}
],
"dateAdded": 1542016208019,
"dateGroupModified": 1542016335149,
"id": "490",
"index": 1,
"parentId": "2",
"title": "Planes"
},
{
"children": ,
"dateAdded": 1542016213955,
"dateGroupModified": 1542016213955,
"id": "491",
"index": 2,
"parentId": "2",
"title": "Ships"
}
],
"dateAdded": 1529656829217,
"dateGroupModified": 1542016213955,
"id": "2",
"index": 1,
"parentId": "0",
"title": "Other Bookmarks"
}
],
"dateAdded": 1541543894421,
"id": "0",
"title": ""
}
]





[
{
"children": [
{
"children": [
{
"dateAdded": 1509653754344,
"id": "459",
"index": 18,
"parentId": "1",
"title": "Test 1",
"url": "https://www.test1.net/"
},
{
"dateAdded": 1509653754369,
"id": "460",
"index": 19,
"parentId": "1",
"title": "Test 2",
"url": "https://www.test2.net/"
}
],
"dateAdded": 1529656829217,
"dateGroupModified": 1538037498559,
"id": "1",
"index": 0,
"parentId": "0",
"title": "Bookmarks Bar"
},
{
"children": [
{
"children": [
{
"children": [
{
"children": ,
"dateAdded": 1542016268115,
"dateGroupModified": 1542016268115,
"id": "496",
"index": 0,
"parentId": "492",
"title": "F40"
},
{
"children": ,
"dateAdded": 1542016288842,
"dateGroupModified": 1542016288842,
"id": "497",
"index": 1,
"parentId": "492",
"title": "Testarossa"
}
],
"dateAdded": 1542016224744,
"dateGroupModified": 1542016288843,
"id": "492",
"index": 0,
"parentId": "489",
"title": "Ferrari"
},
{
"children": ,
"dateAdded": 1542016232765,
"dateGroupModified": 1542016232765,
"id": "493",
"index": 1,
"parentId": "489",
"title": "Toyota"
},
{
"children": ,
"dateAdded": 1542016245690,
"dateGroupModified": 1542016245690,
"id": "494",
"index": 2,
"parentId": "489",
"title": "BMW"
},
{
"children": ,
"dateAdded": 1542016253590,
"dateGroupModified": 1542016253590,
"id": "495",
"index": 3,
"parentId": "489",
"title": "Audi"
}
],
"dateAdded": 1542016199154,
"dateGroupModified": 1542016253590,
"id": "489",
"index": 0,
"parentId": "2",
"title": "Cars"
},
{
"children": [
{
"children": ,
"dateAdded": 1542016326727,
"dateGroupModified": 1542016326727,
"id": "498",
"index": 0,
"parentId": "490",
"title": "Boeing"
},
{
"children": ,
"dateAdded": 1542016335148,
"dateGroupModified": 1542016335148,
"id": "499",
"index": 1,
"parentId": "490",
"title": "Airbus"
}
],
"dateAdded": 1542016208019,
"dateGroupModified": 1542016335149,
"id": "490",
"index": 1,
"parentId": "2",
"title": "Planes"
},
{
"children": ,
"dateAdded": 1542016213955,
"dateGroupModified": 1542016213955,
"id": "491",
"index": 2,
"parentId": "2",
"title": "Ships"
}
],
"dateAdded": 1529656829217,
"dateGroupModified": 1542016213955,
"id": "2",
"index": 1,
"parentId": "0",
"title": "Other Bookmarks"
}
],
"dateAdded": 1541543894421,
"id": "0",
"title": ""
}
]






javascript jquery asynchronous google-chrome-extension






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 11:34

























asked Nov 12 at 8:18









Ben

50110




50110












  • Do not use _.each. It does not work with asynchronous code. _.map them to an array of promises and use Promise.all.
    – Bergi
    Nov 12 at 8:24










  • Promisify only the chrome.bookmarks.getSubTree(bmkNode) function, put nothing else into the new Promise constructor. Then use .then() chaining to work with the result values
    – Bergi
    Nov 12 at 8:26










  • @Bergi I thought we are using _.map when we are expecting to return something? There is no return here..? Could you suggest the function changes below? Thanks
    – Ben
    Nov 12 at 9:05












  • Try to find something useful that could be returned (hint: don't use a global folderItem variable). And since the recursive getTrees call is asynchronous, you will need to return a promise for this thing. Then you will have an array of promises in the end, which is something that Promise.all can work with.
    – Bergi
    Nov 12 at 9:12










  • @Bergi I've edited my example so there is no need for promise also added actual object. Tell me what you think? Thanks
    – Ben
    Nov 12 at 10:47


















  • Do not use _.each. It does not work with asynchronous code. _.map them to an array of promises and use Promise.all.
    – Bergi
    Nov 12 at 8:24










  • Promisify only the chrome.bookmarks.getSubTree(bmkNode) function, put nothing else into the new Promise constructor. Then use .then() chaining to work with the result values
    – Bergi
    Nov 12 at 8:26










  • @Bergi I thought we are using _.map when we are expecting to return something? There is no return here..? Could you suggest the function changes below? Thanks
    – Ben
    Nov 12 at 9:05












  • Try to find something useful that could be returned (hint: don't use a global folderItem variable). And since the recursive getTrees call is asynchronous, you will need to return a promise for this thing. Then you will have an array of promises in the end, which is something that Promise.all can work with.
    – Bergi
    Nov 12 at 9:12










  • @Bergi I've edited my example so there is no need for promise also added actual object. Tell me what you think? Thanks
    – Ben
    Nov 12 at 10:47
















Do not use _.each. It does not work with asynchronous code. _.map them to an array of promises and use Promise.all.
– Bergi
Nov 12 at 8:24




Do not use _.each. It does not work with asynchronous code. _.map them to an array of promises and use Promise.all.
– Bergi
Nov 12 at 8:24












Promisify only the chrome.bookmarks.getSubTree(bmkNode) function, put nothing else into the new Promise constructor. Then use .then() chaining to work with the result values
– Bergi
Nov 12 at 8:26




Promisify only the chrome.bookmarks.getSubTree(bmkNode) function, put nothing else into the new Promise constructor. Then use .then() chaining to work with the result values
– Bergi
Nov 12 at 8:26












@Bergi I thought we are using _.map when we are expecting to return something? There is no return here..? Could you suggest the function changes below? Thanks
– Ben
Nov 12 at 9:05






@Bergi I thought we are using _.map when we are expecting to return something? There is no return here..? Could you suggest the function changes below? Thanks
– Ben
Nov 12 at 9:05














Try to find something useful that could be returned (hint: don't use a global folderItem variable). And since the recursive getTrees call is asynchronous, you will need to return a promise for this thing. Then you will have an array of promises in the end, which is something that Promise.all can work with.
– Bergi
Nov 12 at 9:12




Try to find something useful that could be returned (hint: don't use a global folderItem variable). And since the recursive getTrees call is asynchronous, you will need to return a promise for this thing. Then you will have an array of promises in the end, which is something that Promise.all can work with.
– Bergi
Nov 12 at 9:12












@Bergi I've edited my example so there is no need for promise also added actual object. Tell me what you think? Thanks
– Ben
Nov 12 at 10:47




@Bergi I've edited my example so there is no need for promise also added actual object. Tell me what you think? Thanks
– Ben
Nov 12 at 10:47












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










This is correct solution for this problem, no need for Promise as I thought:



   const getFolders = bmkNode => {
const props = {
menuContainerId: $("#bookmarks")
}

const getTree = (element, bmkNode) => {
let ul = $('<ul data-node="' + bmkNode + '"></ul>')
let li

chrome.bookmarks.getSubTree(bmkNode, folder => {
_.map(folder[0].children, item => {
if (item.url === undefined || item.url === null) {
li = $('<li data-node="' + item.id + '"></li>')
li.append(item.title)

ul.append(li)
if (_.size(item.children) > 0) {
getTree(li, item.id)
}

}
})
element.append(ul)
})
}

getTree(props.menuContainerId, bmkNode)
}





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53258185%2frecursive-function-chrome-bookmarks-loop-appending-ul-li%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote



    accepted










    This is correct solution for this problem, no need for Promise as I thought:



       const getFolders = bmkNode => {
    const props = {
    menuContainerId: $("#bookmarks")
    }

    const getTree = (element, bmkNode) => {
    let ul = $('<ul data-node="' + bmkNode + '"></ul>')
    let li

    chrome.bookmarks.getSubTree(bmkNode, folder => {
    _.map(folder[0].children, item => {
    if (item.url === undefined || item.url === null) {
    li = $('<li data-node="' + item.id + '"></li>')
    li.append(item.title)

    ul.append(li)
    if (_.size(item.children) > 0) {
    getTree(li, item.id)
    }

    }
    })
    element.append(ul)
    })
    }

    getTree(props.menuContainerId, bmkNode)
    }





    share|improve this answer

























      up vote
      0
      down vote



      accepted










      This is correct solution for this problem, no need for Promise as I thought:



         const getFolders = bmkNode => {
      const props = {
      menuContainerId: $("#bookmarks")
      }

      const getTree = (element, bmkNode) => {
      let ul = $('<ul data-node="' + bmkNode + '"></ul>')
      let li

      chrome.bookmarks.getSubTree(bmkNode, folder => {
      _.map(folder[0].children, item => {
      if (item.url === undefined || item.url === null) {
      li = $('<li data-node="' + item.id + '"></li>')
      li.append(item.title)

      ul.append(li)
      if (_.size(item.children) > 0) {
      getTree(li, item.id)
      }

      }
      })
      element.append(ul)
      })
      }

      getTree(props.menuContainerId, bmkNode)
      }





      share|improve this answer























        up vote
        0
        down vote



        accepted







        up vote
        0
        down vote



        accepted






        This is correct solution for this problem, no need for Promise as I thought:



           const getFolders = bmkNode => {
        const props = {
        menuContainerId: $("#bookmarks")
        }

        const getTree = (element, bmkNode) => {
        let ul = $('<ul data-node="' + bmkNode + '"></ul>')
        let li

        chrome.bookmarks.getSubTree(bmkNode, folder => {
        _.map(folder[0].children, item => {
        if (item.url === undefined || item.url === null) {
        li = $('<li data-node="' + item.id + '"></li>')
        li.append(item.title)

        ul.append(li)
        if (_.size(item.children) > 0) {
        getTree(li, item.id)
        }

        }
        })
        element.append(ul)
        })
        }

        getTree(props.menuContainerId, bmkNode)
        }





        share|improve this answer












        This is correct solution for this problem, no need for Promise as I thought:



           const getFolders = bmkNode => {
        const props = {
        menuContainerId: $("#bookmarks")
        }

        const getTree = (element, bmkNode) => {
        let ul = $('<ul data-node="' + bmkNode + '"></ul>')
        let li

        chrome.bookmarks.getSubTree(bmkNode, folder => {
        _.map(folder[0].children, item => {
        if (item.url === undefined || item.url === null) {
        li = $('<li data-node="' + item.id + '"></li>')
        li.append(item.title)

        ul.append(li)
        if (_.size(item.children) > 0) {
        getTree(li, item.id)
        }

        }
        })
        element.append(ul)
        })
        }

        getTree(props.menuContainerId, bmkNode)
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 15 at 8:03









        Ben

        50110




        50110






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53258185%2frecursive-function-chrome-bookmarks-loop-appending-ul-li%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            鏡平學校

            ꓛꓣだゔៀៅຸ໢ທຮ໕໒ ,ໂ'໥໓າ໼ឨឲ៵៭ៈゎゔit''䖳𥁄卿' ☨₤₨こゎもょの;ꜹꟚꞖꞵꟅꞛေၦေɯ,ɨɡ𛃵𛁹ޝ޳ޠ޾,ޤޒޯ޾𫝒𫠁သ𛅤チョ'サノބޘދ𛁐ᶿᶇᶀᶋᶠ㨑㽹⻮ꧬ꧹؍۩وَؠ㇕㇃㇪ ㇦㇋㇋ṜẰᵡᴠ 軌ᵕ搜۳ٰޗޮ޷ސޯ𫖾𫅀ल, ꙭ꙰ꚅꙁꚊꞻꝔ꟠Ꝭㄤﺟޱސꧨꧼ꧴ꧯꧽ꧲ꧯ'⽹⽭⾁⿞⼳⽋២៩ញណើꩯꩤ꩸ꩮᶻᶺᶧᶂ𫳲𫪭𬸄𫵰𬖩𬫣𬊉ၲ𛅬㕦䬺𫝌𫝼,,𫟖𫞽ហៅ஫㆔ాఆఅꙒꚞꙍ,Ꙟ꙱エ ,ポテ,フࢰࢯ𫟠𫞶 𫝤𫟠ﺕﹱﻜﻣ𪵕𪭸𪻆𪾩𫔷ġ,ŧآꞪ꟥,ꞔꝻ♚☹⛵𛀌ꬷꭞȄƁƪƬșƦǙǗdžƝǯǧⱦⱰꓕꓢႋ神 ဴ၀க௭எ௫ឫោ ' េㇷㇴㇼ神ㇸㇲㇽㇴㇼㇻㇸ'ㇸㇿㇸㇹㇰㆣꓚꓤ₡₧ ㄨㄟ㄂ㄖㄎ໗ツڒذ₶।ऩछएोञयूटक़कयँृी,冬'𛅢𛅥ㇱㇵㇶ𥄥𦒽𠣧𠊓𧢖𥞘𩔋цѰㄠſtʯʭɿʆʗʍʩɷɛ,əʏダヵㄐㄘR{gỚṖḺờṠṫảḙḭᴮᵏᴘᵀᵷᵕᴜᴏᵾq﮲ﲿﴽﭙ軌ﰬﶚﶧ﫲Ҝжюїкӈㇴffצּ﬘﭅﬈軌'ffistfflſtffतभफɳɰʊɲʎ𛁱𛁖𛁮𛀉 𛂯𛀞నఋŀŲ 𫟲𫠖𫞺ຆຆ ໹້໕໗ๆทԊꧢꧠ꧰ꓱ⿝⼑ŎḬẃẖỐẅ ,ờỰỈỗﮊDžȩꭏꭎꬻ꭮ꬿꭖꭥꭅ㇭神 ⾈ꓵꓑ⺄㄄ㄪㄙㄅㄇstA۵䞽ॶ𫞑𫝄㇉㇇゜軌𩜛𩳠Jﻺ‚Üမ႕ႌႊၐၸဓၞၞၡ៸wyvtᶎᶪᶹစဎ꣡꣰꣢꣤ٗ؋لㇳㇾㇻㇱ㆐㆔,,㆟Ⱶヤマފ޼ޝަݿݞݠݷݐ',ݘ,ݪݙݵ𬝉𬜁𫝨𫞘くせぉて¼óû×ó£…𛅑הㄙくԗԀ5606神45,神796'𪤻𫞧ꓐ㄁ㄘɥɺꓵꓲ3''7034׉ⱦⱠˆ“𫝋ȍ,ꩲ軌꩷ꩶꩧꩫఞ۔فڱێظペサ神ナᴦᵑ47 9238їﻂ䐊䔉㠸﬎ffiﬣ,לּᴷᴦᵛᵽ,ᴨᵤ ᵸᵥᴗᵈꚏꚉꚟ⻆rtǟƴ𬎎

            Why https connections are so slow when debugging (stepping over) in Java?