Retry request until API returns proper value then continue with further requests

I need to retry particular request in Postman to wait until task will be unlocked.
I don’t know how long it will take, it may be from 1 sec up to 1min.
Below script seems to not always be working due to asynchronous behaviour.

What kind of solution would you recommend here?

function checkIfTaskIsLocked(responseJson) {
    if(responseJson.items && responseJson.items(taskId) {
    if (responseJson.items[taskId].isLocked === false {
    return true;
    } else {
        console.log("task is locked");
        return false
    }
} else {
    console.log("task not found")
    return false;
}
}

if(!checkIfTaskIsLocked(responseJson)) {
    console.log("retrying request")
    setTimeout(function() {
        pm.execution.setNextRequest(pm.info.requestName)
    }, 10000)
}