Found Issue on Promise Library Created by evaera (possible issue)

This is an issue with the Promise Library Created by evaera,. (Im not sure if im using it correctly but this is an issue I found)

ISSUE: resolve() does NOT stop the execution of the lines after it immediately. This is problematic when the promise have to resolve() inside a loop or if you are trying to stop a function by using resolve(). I do not know how Promises works internally so this might be caused by that.

Consider the ff. code:

--Services
local ServerScriptService_SVC = game:GetService("ServerScriptService")
local ReplicatedStorage_SVC = game:GetService("ReplicatedStorage")

local Promise_MSCPT = require(ReplicatedStorage_SVC:WaitForChild("Promise_MSCPT"))
local ProfileStore =  require(ServerScriptService_SVC:WaitForChild("ProfileStore"))

local function setupProfileStoreMSCPT_FUNC()
	return Promise_MSCPT.new(function(resolve,reject,onCancel)

		for retryCount = 1,3,1 do
			print("This retry number: "..tostring(retryCount))
			task.wait(2) --This is usually the time it takes to load ProfileStore (Datastore manager I use)
			print(ProfileStore.DataStoreState) 
			if ProfileStore.DataStoreState == "Access" then --This is basically just checking if the Datastore Manager is ready for use
				resolve() --This returns to the original promise sequence then prints "SETUP DONE" but then it prints THIS IS PRINTED AFTER THE resolve()
				print("THIS IS PRINTED AFTER THE resolve()") --I don't think this is suppose to print, then after that it moves on to retry number 2
			end
		end

		print("ProfileStore failed to load")
		reject()
	end)	
end

local function StartSetupSequence_FUNC()

	local SetupServerSequence_PMSE = setupProfileStoreMSCPT_FUNC()
		:andThen(function()
			return Promise_MSCPT.new(function(resolve)
				print("SETUP IS DONE")
				resolve()
			end)
		end)

end

StartSetupSequence_FUNC()

OUTPUT

  22:33:18.021  This retry number: 1  -  Server - Script:12
  22:33:19.204  [ProfileStore]: Roblox API services available - data will be saved  -  Server - ProfileStore:2098
  22:33:20.035  Access  -  Server - Script:14
  22:33:20.036  SETUP IS DONE  -  Server - Script:31 
  22:33:20.036  THIS IS PRINTED AFTER THE resolve()  -  Server - Script:17
  22:33:20.037  This retry number: 2  -  Server - Script:12

In this example,
The resolve() does work as evident by SETUP IS DONE, but then it comes back to print THIS IS PRINTED AFTER THE resolve() and then continues the loop as evident by This retry number: 2, now this is also weird because it stops there, and does not print print(ProfileStore.DataStoreState) , and I think that is because of the task.wait(), so I think that resolve() is NOT able stop the execution of the lines after it immediately. P.S sorry If I’m wording and using the wrong terms to describe this but I do hope you get the idea of it.

I did find a solution for this by Adding a task.wait() after the resolve()

--Services
local ServerScriptService_SVC = game:GetService("ServerScriptService")
local ReplicatedStorage_SVC = game:GetService("ReplicatedStorage")

local Promise_MSCPT = require(ReplicatedStorage_SVC:WaitForChild("Promise_MSCPT"))
local ProfileStore =  require(ServerScriptService_SVC:WaitForChild("ProfileStore"))

local function setupProfileStoreMSCPT_FUNC()
	return Promise_MSCPT.new(function(resolve,reject,onCancel)

		for retryCount = 1,3,1 do
			print("This retry number: "..tostring(retryCount))
			task.wait(2) --This is usually the time it takes to load ProfileStore (Datastore manager I use)
			print(ProfileStore.DataStoreState) 
			if ProfileStore.DataStoreState == "Access" then --This is basically just checking if the Datastore Manager has laoded
				resolve()
				task.wait() ----!!!!Added a wait time here to allow resolve() to stop the execution
				print("THIS IS PRINTED AFTER THE resolve()") --This doesn'y print anymore, also it does not go to retry no. 2
			end
		end

		print("ProfileStore failed to load")
		reject()
	end)	
end

local function StartSetupSequence_FUNC()

	local SetupServerSequence_PMSE = setupProfileStoreMSCPT_FUNC()
		:andThen(function()
			return Promise_MSCPT.new(function(resolve)
				print("SETUP IS DONE")
				resolve()
			end)
		end)

end

StartSetupSequence_FUNC()

OUTPUT

  23:16:03.345  This retry number: 1  -  Server - Script(TrialSolution):12
  23:16:04.529  [ProfileStore]: Roblox API services available - data will be saved  -  Server - ProfileStore:2098
  23:16:05.352  Access  -  Server - Script(TrialSolution):14
  23:16:05.352  SETUP IS DONE  -  Server - Script(TrialSolution):32

In this I added a task.wait() to allow the resolve() to stop the execution and it works it didn’t continue with the code after resolve

FOLLOW-UP QUESTIONS:

  1. I also considered adding a return after the resolve() but I don’t think that is the best way as it conflict with the returned value of resolve(), or will it?

  2. Is this an intended behavior? or is this a bug? I tried to search the net but couldn’t find anything talking about this.

TLDR: resolve() is NOT able stop the execution of the lines after it immediately, this can be problematic in loops, adding a wait in this case task.wait() somehow allows resolve() to stop the execution. So my conclusion is resolve() needs some time to stop the execution and it shouldn’t be use to try to stop a function like this

local function exampleFunction(value)
       return Promise_MSCPT.new(function(resolve,reject,onCancel)
               if value == true then resolve() end --This won't stop the function immediately

              print ("value is not true") --This will still run even if the value is true because resolve() can't stop the function immediately
       end
end

This is a repo of the possible bug if you’d like to test it
PROMISE possible bug REPO.rbxl (103.4 KB)

I am confused and also a newbie to scripting so I might have mistakes in this post, I am trying to understand it. Looking forward for feedbacks, Thank you!

Don’t make promises when they’re bound to be broken.

I’m not too sure I understand what you’re trying to say is, can you please elaborate on it?

2 Likes