c0balt60/promise_child

Promises a child appears with an Instance

promise-child

Promises a child appears within an Instance. Has 3 functions:

function promiseChild(parent: Instance, childName: string | number): Promise<Instance>

function promiseChildOfClass<T = Instance>(parent: Instance, className: string): Promise<T>

function promiseChildWhichIsA<T = Instance>(parent: Instance, className: string): Promise<T>

promiseChild Promise resolves when the given childName appears inside an instance promiseChildOfClass looks for the given className promiseChildWhichIsA looks for an instance which follows the format IsA(class)

Example:

local promiseChildOfClass = require(...)

game:GetService("Players").PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(Char)
        local humanoid = promiseChildOfClass(char, "Humanoid"):await()

        -- execute code after humanoid is verified to be in character
    end)
end)

Since it is implemented using Promises, these are cancellable requests!

local Lighting = game:GetService("Lighting")
local skyGetter = promiseChildOfClass(Lighting, "Sky")

-- if skyGetter didn't already resolve at the end of 5 seconds, cancel it
local skyTimeout = Promise.delay(5):andThen(function() skyGetter:cancel() end)

local sunRaysGetter = promiseChildOfClass(Lighting, "SunRaysEffect")
sunRaysGetter
    :now()
    :andThen(function()
        print("We got our SunRaysEffect synchronously!")
    end)
    :catch(function(...)
        print("We're going to have to wait for it to show up!")
    end)