Promises and Why You Should Use Them

OMG its JavaScript/like promises

1 Like

@evaera, Just want to verify that it is the init.lua file in your repo that is the Promise.lua file we should be using?

1 Like

hey! thanks for this wonderful open source module you posted but for some reason when I try to run this, it says its an unhandled promise rejection. Whats wrong with this code?

– Code

local tabNaruto = {Naruto = 5}

local function promiseCal(callback)

Promise.try(function()

return callback() and “ok” or error(“Oh an error!”)

end)

end

promiseCal(function()

print(tabNaruto.Naruto.shirt)

end):andThen(function(message)

print(message)

end):catch(function(err)

print(string.format(“Error: %s”, err))

end)

The Roblox community has benefited greatly from promises and coroutines. They provide us the tools we need to construct a far more dependable manner of creating code and accessing data. Furthermore, it is open source, which means that anybody may contribute to the community and help keep it up to date.

1 Like

hey. i’m using your module in my roblox game and I have a small problem. When I cancel a running promise, it sends an error “cannot resume dead coroutine”, Idk how to fix this, it doesnt give me any problem but it bother me to check the output or something like this.
Example

local promise = require(...)
local _promise = promise.new(function(resolve, reject, onCancel)
    wait(5)
    print("Resolved")
    resolve()
end)

wait(2)
_promise:cancel() --> cancel the coroutine but after the _promise wait finishes, it gives me an error "cannot resume dead coroutine"

If you could help me with that problem thx!

You added a wait(2) before canceling and by that time your promise is already settled, meaning no point in canceling.
Documentation: Promise | roblox-lua-promise

Yeah Ik I did it to see the difference
The problem isn’t that, the problem is when I’m cancelling a promise which have a wait or which is yielding, I get an error “cannot resume dead coroutine”, it is the same for coroutines

This is because you’re using legacy wait, which doesn’t check whether the thread is dead or not before trying to resume the thread. This causes the error, as promises kill the thread before wait is done yielding. In this case, you can just change wait to task.wait and it should work without outputting the error.

This happens with other things too unfortunately, like with RemoteFunctions, BindableFunctions for example, since they don’t either check whether the thread is dead or not before trying to resume the thread.

Here’s a bug report for it.

1 Like