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);
});