Skip to content

Commit

Permalink
fix!: use promise:new instead of promise.new
Browse files Browse the repository at this point in the history
In Lua convention, create an object always carry `self`, so we must
break change for further development.

Create an promise object:
```lua
local p = promise:new(function(resolve, reject) end)
-- or
local p = promise(function(resolve, reject) end)
```
  • Loading branch information
kevinhwang91 committed Jan 5, 2023
1 parent 25ac2dd commit 3f6dcb2
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 16 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ Summary up the API different from JavaScript.

| JavaScript | Lua |
| --------------------------------------------------- | ----------------------------------------------- |
| `new Promise` | `Promise.new`/`Promise` |
| `new Promise` | `Promise:new`/`Promise` |
| `Promise.then` | `Promise:thenCall`, `then` is language keyword |
| `Promise.catch` | `Promise:catch` |
| `Promise.finally` | `Promise:finally` |
Expand Down
2 changes: 1 addition & 1 deletion examples/demo.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ local function setTimeout(callback, ms)
end

local function defuse(ms)
return promise.new(function(resolve, reject)
return promise:new(function(resolve, reject)
setTimeout(function()
resolve(ms)
end, ms)
Expand Down
2 changes: 1 addition & 1 deletion lua/async.lua
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ function Async.sync(executor)
local isCallable, fn = utils.getCallable(executor, typ)
assert(isCallable, 'a callable table or function expected, got ' .. typ)
injectENV(fn)
return promise.new(function(resolve, reject)
return promise:new(function(resolve, reject)
local co = coroutine.create(typ == 'function' and executor or function()
return executor()
end)
Expand Down
22 changes: 10 additions & 12 deletions lua/promise.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ local REJECTED = 3
---@field err? PromiseAsyncError
local Promise = setmetatable({_id = promiseId}, {
__call = function(self, executor)
return self.new(executor)
return self:new(executor)
end
})
Promise.__index = Promise
Expand Down Expand Up @@ -241,11 +241,9 @@ resolvePromise = function(promise, value)
end
end

function Promise.new(executor)
function Promise:new(executor)
utils.assertType(executor, 'function')
---@type Promise
local o = setmetatable({}, Promise)

local o = self == Promise and setmetatable({}, self) or self
o.state = PENDING
o.result = nil
o.queue = {}
Expand All @@ -258,7 +256,7 @@ function Promise.new(executor)
end

function Promise:thenCall(onFulfilled, onRejected)
local o = Promise.new(noop)
local o = self.new(Promise, noop)
table.insert(self.queue, {o, onFulfilled, onRejected})
if self.state ~= PENDING then
handleQueue(self)
Expand Down Expand Up @@ -292,7 +290,7 @@ function Promise.resolve(value)
if Promise.isInstance(value, typ) then
return value
else
local o = Promise.new(noop)
local o = Promise:new(noop)
local thenCall = getThenable(value, typ)
if thenCall then
wrapExecutor(o, thenCall, value)
Expand All @@ -305,7 +303,7 @@ function Promise.resolve(value)
end

function Promise.reject(reason)
local o = Promise.new(noop)
local o = Promise:new(noop)
o.state = REJECTED
o.result = reason
handleRejection(o)
Expand All @@ -314,7 +312,7 @@ end

function Promise.all(values)
utils.assertType(values, 'table')
return Promise.new(function(resolve, reject)
return Promise:new(function(resolve, reject)
local res = {}
local cnt = 0
for k, v in pairs(values) do
Expand All @@ -337,7 +335,7 @@ end

function Promise.allSettled(values)
utils.assertType(values, 'table')
return Promise.new(function(resolve, reject)
return Promise:new(function(resolve, reject)
local res = {}
local cnt = 0
local _ = reject
Expand All @@ -362,7 +360,7 @@ end

function Promise.any(values)
utils.assertType(values, 'table')
return Promise.new(function(resolve, reject)
return Promise:new(function(resolve, reject)
local cnt = 0
local function rejectAggregateError()
if cnt == 0 then
Expand All @@ -386,7 +384,7 @@ end

function Promise.race(values)
utils.assertType(values, 'table')
return Promise.new(function(resolve, reject)
return Promise:new(function(resolve, reject)
for _, p in pairs(values) do
Promise.resolve(p):thenCall(function(value)
resolve(value)
Expand Down
2 changes: 1 addition & 1 deletion typings/promise.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ local Promise = {}
---a resolve callback used to resolve the promise with a value or the result of another promise,
---and a reject callback used to reject the promise with a provided reason or error.
---@return Promise promise A new Promise.
function Promise.new(executor) end
function Promise:new(executor) end

---Attaches callbacks for the resolution and/or rejection of the Promise.
---@param onFulfilled? fun(value: any): any The callback to execute when the Promise is resolved.
Expand Down

0 comments on commit 3f6dcb2

Please sign in to comment.