What would be better to use for checking if GetAsync was Successful?

Which would be more efficient when checking if GetAsync was Successful?

I am talking about the return result parts of the code

if success then -- if successful
	if result then -- if Data
		return result -- returns result
	else -- If no Data
		return table.clone(self.Data) -- returns new Data
	end
else -- if Failed
	return -- returns nothing
end
if success then -- if successful
	return result or table.clone(self.Data) -- Result or new Data
else -- if Failed
	return -- returns nothing
end

Sorry if the Topic is a bit misleading, or too vague btw.

2 Likes

The second might be slightly more performant, but it would be so small that it wouldn’t matter which one you use. Choose which one is more readable to you.

1 Like

you can go smaller if u want

i gues you are using a pcall.
when u use a return inside the pcall the 2nd output will become the returned value

local succes,returnedvalue= pcall (function()
	return "test"
end)
if not succes then print(returnedvalue)end 

I think the question is about how to handle the return better, splitted in an if statement or by using or.
Adding the not makes no difference at all for the question:

if not success then
	return -- nothing
else
	return result or table.clone(self.Data)
end

if success then
	return result or table.clone(self.Data)
else
	return -- nothing
end

I’d typically use a guard statement pattern for this. E.g:


if not success then
    return 
end

if not result then
    return table.clone(self.Data)
end

return result

I find this to be the most readable as it reduces indentation and separates the different checks / logic clearly.

Late Replies but.

Good to Know :slight_smile:


Nahh Really???

my pcall()

success, result = pcall(self.DataStore.GetAsync, self.DataStore, Key)

Its not Important however, Is whats I do after that is.


Yes

Yes

Y E S. . .


That is neater, It might as well be cleaner :slight_smile:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.