0

Hello I have this function which fills an array for me. When I output the array in the for loop it contains elements. However, when I return below with resolve and then call the function and return with then the array seems to be empty.

How can I return this array correctly?

   getUrlsOfFolder(id){
        return new Promise(async (resolve) =>{
            let urls = new Array();
            chrome.bookmarks.getSubTree(id,(searchResult) => {
                for(const key of searchResult){
                    if(key.children){
                        let children = key.children;
                        for(const child of children){
                            if(child.url){
                                urls.push(child.url);
                            }
                        }
                    }
                }
            });
            resolve(urls)
        });
    }
Or Assayag
  • 5,662
  • 13
  • 57
  • 93
  • 3
    [The bookmarks.getSubTree() method **asynchronously** retrieves a bookmarks.BookmarkTreeNode, given its ID.](https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/bookmarks/getSubTree) — You're resolving the promise before the `getSubTree` call has resolved. – Quentin Jan 05 '21 at 17:29
  • 1
    [Never pass an `async function` as the executor to `new Promise`](https://stackoverflow.com/q/43036229/1048572) - and you're not using `await` anyway! – Bergi Jan 05 '21 at 17:31

0 Answers0