Difference between promise.new and promise.async

Hello,

I’m currently learning about promise and trying them out. I tried different codes and I think I’m getting used to it. But I don’t understand why in this code, even if the promise is cancelled because it’ll not print “Promise done”, it won’t print “Cancel”. Unless I change promise.new to promise.async on line HERE. Anyone knows why ?

    local Promise = require(game.Promise) 
    function a()
            return Promise.async(function(resolve)
                print("promise start")
                resolve()
            end):andThen(function()
            return Promise.new(function(resolve, reject, onCancel) --< HERE
                if onCancel(function()
                    print("Cancel")
                end) then return end
                print("waiting")
                wait(7)
                print("waiting ended")
                resolve()
            end)
        end):andThen(function()
            print("promise done")
        end)
    end
    local promise = a()
    wait(5)
    print("cancel the promise !")
    promise:cancel()
2 Likes

I don’t know which implementation of Promise you are using, but it seems to be the evaera one. If so, you probably need to use the most recent version, because async is deprecated and defer should be used instead. This is the difference between defer and new according to the manual

The same as Promise.new, except execution begins after the next Heartbeat event.

When I tested your code, I got the same result with both new and defer.

2 Likes

I’m indeed using evaera’s promise module and the most recent version, but there is a line in the module saying :

Promise.async = Promise.defer

So me using .async shouldn’t be a problem. And also after trying my code again, it seems to be working fine now for some obscur reasons lol :thinking:

Anyway thanks for taking the time to try my code and help me ^^

1 Like