def query_in_sync(func):
    @wraps(func)
    def wrapper(api, data, error, n, last):
        api.req_id = max(api.req_id, n)
        result = func(api, data, last)
        if last:
            api.gateway.on_query(api.api_name, n, result)
    return wrapper