Help with writing a loop in postman tests

I have a request (you can use it):


There is a test with a loop, which will be canceled if the server returns cursor = 0

pm.variables.set('cursorValue', pm.response.json().cursor);
let finalCursor = "0";
let cursor = pm.variables.get('cursorValue');

while (cursor !== finalCursor && cursor !== null && cursor !== undefined) {
        url: pm.variables.get("API host") + "/" + pm.variables.get("APP ID") + "/" + pm.variables.get("REST API Key") + "/" + 'hive/Management/list/keys?filterPattern=*&cursor=' + cursor + "&" + 'pageSize=5',
        method: 'GET',
    }, function (err, res) {
        if (err) {
        } else {
            cursor = res.json().cursor;
            pm.globals.set('cursorValue', cursor);
            pm.test(`Cursor equals ${cursor}`, () => {

However, when I send a request, first of all, the postman completely freezes (only a restart helps). Secondly, the first request is sent with cursor=0, but the next ones are all sent with cursor=360, but after the second one the cursor value should be different. How to fix this? enter image description here

For example:
1 iteration: A request is sent with cursor=0;
response from the server "cursor": "360"
2 iteration: Request is sent with cursor=360
Response from the server "cursor": "86"
3 iteration: Request sent from cursor=86
Response from the server "cursor": "415"
4 iteration: Request is sent from cursor=415
Response from the server "cursor": "10"
5 iteration: Request sent from cursor=10
Response from the server "cursor": "0" 
The test is over

Please help in any way you can