Convert wait to Promises

Is there any way to write this code using Promises. What I want is to avoid the use of wait by using Promises. Thanks for your help.

while true do
    wait(2)
    print("Create object")
end

I’m not entirely sure what “Promise” is like in Javascript but looking at the documentation of it, it seems to be similar to pcall() and you definitely don’t want to use pcall() because it’ll create immense lag if ran repeatedly.

There’s nothing wrong with the example you showed. If you want to know if a part was successfully instanced into Workspace, then that would be unnecessary since that will never fail.

Why do you want to avoid using wait(2)? There is nothing wrong with using it in your code.

I refer to these promises.

You are right, but I would like to know if it is possible for learning purposes.

I really don’t understand what you’re trying to achieve, could you be more specific? Are you trying to instance multiple objects asynchronously? Why do you need to represent an object which exists in the future when Roblox does that already. By Object I think you’re referring to Part Instances. Those aren’t added to Workspace immediately, but rather added on the first physics simulation.

In JavaScript you’d create a Promise-based “wait” like so:

const wait = (time = 1000) => new Promise(resolve => setTimeout(resolve, time))

console.log("Hello")
await wait(3000)

console.log("world!")

Perhaps you can adapt that to work with Lua? There’s a delay method that works similarly to JavaScript’s setTimeout.

1 Like

:vulcan_salute: