0

The below data function is called by a get route. The problem I have is that I can't figure out why the images array remains empty on return images but shows as correctly populated at console.log(images) in my recursive start(node) function -- which performs a DFS by repeatedly calling the Microsoft Graph API and allocates img objects to the images array. I initialise the traversal after res contains the object tree.

I assumed that return images would execute only after the recursion is complete and images is populated, but that doesn't look to be the case. I think it's an asynchrony issue which I'm not too familiar with, so would appreciate any help or solution.

shelfdb.data = (accessToken) => {
    const token = accessToken;
    const endpoint = 'https://graph.microsoft.com/v1.0/sites/webgroup.sharepoint.com,23e7ef7a-a529-4dde-81ba-67afb4f44401,0fa8e0f7-1c76-4ad0-9b6e-a485f9bfd63c/drive/items/01GNYB5KPQ57RHLPZCJFE2QMVKT5U3NYY3/children'

    var res = callMsGraph(token, endpoint);

    var images = []

    function start(node) {
        if(node.value) {
            node.value.forEach(function(child) {
                var end = 'https://graph.microsoft.com/v1.0/sites/webgroup.sharepoint.com,23e7ef7a-a529-4dde-81ba-67afb4f44401,0fa8e0f7-1c76-4ad0-9b6e-a485f9bfd63c/drive/items/' + child.id + '/children';
                var result = callMsGraph(token, end);

                result.then(function(result) {
                    if (result.value.length > 0) {
                        if ('image' in result.value[0]) {
                            result.value.forEach(function(imgChild) {
                                let img = {
                                    'name': imgChild.name,
                                    'job': imgChild.parentReference.path.split("/")[6],
                                    'path': imgChild.webUrl
                                }
                                images.push(img);

                                console.log('images object:')
                                console.log(images);
                            })
                            
                            return;
                        }
                    }

                    start(result);
                })

            })
        }
    }

    res.then(function(result) {
        start(result);
    })

    return images;

};
TabulaRasa
  • 61
  • 1
  • 10
  • 3
    asynchronous 101 - Your code does not sit around and wait for the then() to execute. – epascarello Jul 12 '22 at 18:08
  • https://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron – epascarello Jul 12 '22 at 18:10
  • Everything you ever wanted to know, and more, [here](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call). – jarmod Jul 12 '22 at 18:14
  • Does this answer your question? [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – ggorlen Jul 12 '22 at 18:37
  • Could someone hold my hand with this? I'm having trouble applying start(node) as a callback and finding where to place `return images`. – TabulaRasa Jul 13 '22 at 14:55
  • making the parent `data` function `async` and `await start(res);` before `return images` still gives me an empty variable. – TabulaRasa Jul 13 '22 at 17:00

0 Answers0