Promise Shared โ
Promise is a module to transform callback-style functions into Promise-based operations, allowing for more elegant handling of asynchronous code.
JO Functions โ
jo.promise.new() โ
Creates a new Promise that wraps a callback function
This utility transforms callback-style functions into Promise-returning functions
Syntax โ
lua
jo.promise.new(cb, ...)
Parameters โ
cb
: function
The callback function to be wrapped in a Promise
...
: mixed
Arguments to pass to the callback function
Return Value โ
Type : ...
The resolved values from the Promise
Example โ
lua
-- Example: Convert a callback-based function to promise-based
local function fetchDataWithCallback(id, callback)
-- Simulate async operation
local id = "TheUniqueID"
jo.timeout.delay(id, 1000, function()
local result = { id = id, name = "Item " .. id }
callback(result)
end)
end
-- Using jo.promise to convert the callback to a promise
local result = jo.promise.new(function(resolver)
fetchDataWithCallback(123, resolver)
end)
-- Now 'result' contains the data directly, without needing a callback
print("Fetched item: " .. result.name)