Opnion on "Run without yielding" function

So, I searched around a while ago, how to make sure a function doesn’t yield. The solution I saw what this:

local function doesYield(func, ...)
    local args = ...;
    local yields = true
    coroutine.wrap(function()
        func(args)
        yields = false
    end)()
    return yields
end

And this works, but I can’t return arguments from it and it looks weird.

Now randomly I thought about using coroutine.status to check that, and now this is my attempt at writing this:

local function RunNoYield(func, ...)
    local args = ...;

    local toReturn;

    local thread = coroutine.create(function()
        toReturn = table.pack(func(arguments))
    end)
    coroutine.resume(thread)
    
    local status = coroutine.status(thread)
    if status ~= "dead" then
        error("Function yielded! Not allowed!")
        --\\ can't figure out a way of stopping the function completely as of the moment.
    end
    return table.unpack(toReturn)
end

And I would like some feedback and some ideas too.

local function doesYield(func, callback, ...)
    local args = {...}
    local yields = true
    coroutine.wrap(function()
        local return_args = table.pack(func(table.unpack(args)))
        yields = false
        callback(table.unpack(return_args))
    end)()
    return yields
end

Also here’s a Roblox variant of no yield:

3 Likes

Oh thanks for the discovery! I’ll definitely be looking onto these files!