Recursive function Chrome Bookmarks loop & appending










1















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) => )
)



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 '18 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 '18 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 '18 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 '18 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 '18 at 10:47















1















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) => )
)



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 '18 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 '18 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 '18 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 '18 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 '18 at 10:47













1












1








1








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) => )
)



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) => )
)



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 '18 at 11:34







Ben

















asked Nov 12 '18 at 8:18









BenBen

52110




52110












  • 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 at 10:47












1 Answer
1






active

oldest

votes


















0














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 )
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',
    autoActivateHeartbeat: false,
    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









    0














    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 )
    element.append(ul)
    )


    getTree(props.menuContainerId, bmkNode)






    share|improve this answer



























      0














      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 )
      element.append(ul)
      )


      getTree(props.menuContainerId, bmkNode)






      share|improve this answer

























        0












        0








        0







        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 )
        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 )
        element.append(ul)
        )


        getTree(props.menuContainerId, bmkNode)







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 15 '18 at 8:03









        BenBen

        52110




        52110



























            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.




            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

            Use pre created SQLite database for Android project in kotlin

            Darth Vader #20

            Ondo