-4

I have some problems with the asynchronous in javascript. There is a function(next) that repeatedly sending REST API requests to a server. The functions will call itself until the root ID is found. You can find console.log('the root ID is reached!') at the spot. After that, I need the last request ID as the returned result of the function(next) but instead, it returns "undefined".((

function next(initId) {
       return requestHttp('GET','https://www.wrike.com/api/v4/folders/' + initId)
        .then(function(response: string) {
            let folderObj =  JSON.parse(response);
            initId = folderObj.data[0].parentIds;
            if (folderObj.data[0].parentIds === WebHook.rootId) {
               console.log('the root ID is reached!');
               return folderObj.data[0].id;
            } else {
                next(initId); 
            }   
        }, function(error) {
            return error;
        });      
}

next(obj.data[0].parentIds).then(function(response) {
    console.log(response);
}).catch(function(err) {
    console.log(err);
});

3 Answers3

1

According to Wrike.com api, you don't have to do this to find the root folder id. https://developers.wrike.com/documentation/api/methods/get-folder

[GET] /folders/{folderId} returns a parentIds property which is an array of parent folders ids.

I don't have an account but I'm quite sure that either the first or the last one in this array (depending on the order) is the ROOT folder id you wan't to find.

Edit

I'm not really sure what you're trying to do but in the meantime, there are several essential errors to fix in your code :

First, here is the right way to return a chained promise

function next(initId) {
    let promise = new Promise(function(resolve, reject) {
    requestHttp('GET','https://www.wrike.com/api/v4/folders/' + initId)
        .then(function(response) {
            if(/*condition to resolve promise*/) {
                resolve(/*with something*/);
            }
            else {
                reject(/*with an error*/);
                // or return another Promise
            }
        })
        .catch(error => reject(error));
    });
    return promise;            
}

Then, the next method takes a parameter called initId so another developer (like us) does not think it's an array of ids.

I mean, it's quite confusing. It works because the parentIds property is an array and the .toString() of this array returns id1,id2,...

Wrike api supports it but you should use ids.join(',') instead.

Stephane Janicaud
  • 3,531
  • 1
  • 12
  • 18
0

Try return Promise.resolve(initId) instead of next(initId).

Eggon
  • 2,032
  • 2
  • 19
  • 38
0
function next(initId, taskId) {
    let promise = new Promise(function(resolve, reject) {
     (async function nextRequest (){   
     await requestHttp('GET','https://www.wrike.com/api/v4/folders/' + initId)
            .then(function(response: any) {
                let folderObj = JSON.parse(response);
                initId = folderObj.data[0].parentIds[0];
                if (initId == WebHook.rootId) {
                    resolve('resolve');
                }
                else {
                    nextRequest();
                }
            })
            .catch(error => reject(error));
        }());
    });
    return promise;            
}

next(obj.data[0].parentIds, true).then(function(response) {
    console.log(response);
}).catch(function(err) {
    console.log(err);
}); 

the answer to the question