Skip to content

Commit

Permalink
rename promise:thenCall to promise:and_then
Browse files Browse the repository at this point in the history
  • Loading branch information
izelnakri committed Sep 2, 2024
1 parent 1a40fd9 commit 1395e08
Show file tree
Hide file tree
Showing 39 changed files with 329 additions and 330 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ local Promise = require("promise")

local promise = Promise:new(function(resolve, reject)
resolve("some result")
end):thenCall(function(result)
end):and_then(function(result)
vim.print("this gets called with some result: " .. result)
end):catch(function(err)
vim.print("no error here, this doesnt get called")
Expand Down Expand Up @@ -60,7 +60,7 @@ correct and memory efficient, handles edge cases like stack overflow on large co
-- promise example
local promise = Promise:new(function(resolve, reject)
resolve({ last_name = "Nakri" })
end):thenCall(function(result)
end):and_then(function(result)
vim.print("last_name should be: " .. result.last_name)
end)
local me = await(promise)
Expand Down
4 changes: 2 additions & 2 deletions lua/promise/all.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ return function(list)
end

for i, item in ipairs(list) do
if type(item) == "table" and type(item.thenCall) == "function" then
if type(item) == "table" and type(item.and_then) == "function" then
item
:thenCall(function(value)
:and_then(function(value)
results[i] = value
remaining = remaining - 1
check_completion()
Expand Down
8 changes: 4 additions & 4 deletions lua/promise/all_settled.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ return function(list)
end

for i, item in ipairs(list) do
if type(item) == "table" and type(item.thenCall) == "function" then
item:thenCall(function(value)
if type(value) == "table" and type(value.thenCall) == "function" then
value:thenCall(function(resolvedValue)
if type(item) == "table" and type(item.and_then) == "function" then
item:and_then(function(value)
if type(value) == "table" and type(value.and_then) == "function" then
value:and_then(function(resolvedValue)
settle("fulfilled", i, resolvedValue)
end, function(reason)
settle("rejected", i, reason)
Expand Down
8 changes: 4 additions & 4 deletions lua/promise/auto.lua
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ return function(inputs)

-- If no dependencies, run the task immediately
if #taskDependencies == 0 then
local promise = Promise.resolve():thenCall(taskFn):thenCall(function(result)
local promise = Promise.resolve():and_then(taskFn):and_then(function(result)
results[taskName] = result
return result
end)
Expand All @@ -78,10 +78,10 @@ return function(inputs)

-- Run task after all dependencies are resolved
local promise = Promise.all(dependencyPromises)
:thenCall(function(depResults)
:and_then(function(depResults)
return taskFn(unpack(depResults))
end)
:thenCall(function(result)
:and_then(function(result)
results[taskName] = result
return result
end)
Expand All @@ -98,7 +98,7 @@ return function(inputs)

-- Wait for all tasks to complete
Promise.all(allPromises)
:thenCall(function()
:and_then(function()
resolve(results)
end)
:catch(function(err)
Expand Down
2 changes: 1 addition & 1 deletion lua/promise/build_task.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ return function(promise_returning_func, ...)
end

local result = promise_returning_func(unpack(target_arguments))
if type(result) == "table" and type(result.thenCall) == "function" then
if type(result) == "table" and type(result.and_then) == "function" then
return result
end

Expand Down
8 changes: 4 additions & 4 deletions lua/promise/hash.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
local function resolveNestedPromises(value)
local Promise = require("promise")

if type(value) == "table" and type(value.thenCall) == "function" then
if type(value) == "table" and type(value.and_then) == "function" then
-- If the value is a promise, resolve it and recursively resolve any nested values
return value:thenCall(resolveNestedPromises)
return value:and_then(resolveNestedPromises)
elseif type(value) == "table" then
-- If the value is a table, recursively resolve all its entries
local keys = {}
Expand All @@ -13,7 +13,7 @@ local function resolveNestedPromises(value)
table.insert(promises, resolveNestedPromises(v))
end

return Promise.all(promises):thenCall(function(resolvedValues)
return Promise.all(promises):and_then(function(resolvedValues)
local resolvedTable = {}
for i, key in ipairs(keys) do
resolvedTable[key] = resolvedValues[i]
Expand All @@ -36,7 +36,7 @@ return function(hashTable)
for key, promise in pairs(hashTable) do
remaining = remaining + 1
resolveNestedPromises(promise)
:thenCall(function(value)
:and_then(function(value)
results[key] = value
remaining = remaining - 1
if remaining == 0 then
Expand Down
8 changes: 4 additions & 4 deletions lua/promise/hash_settled.lua
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
local function settleNestedPromises(value)
local Promise = require("promise")

if type(value) == "table" and type(value.thenCall) == "function" then
if type(value) == "table" and type(value.and_then) == "function" then
-- Handle promise-like objects
return value
:thenCall(function(resolvedValue)
:and_then(function(resolvedValue)
return settleNestedPromises(resolvedValue)
end)
:catch(function(reason)
Expand All @@ -19,7 +19,7 @@ local function settleNestedPromises(value)
table.insert(promises, settleNestedPromises(v))
end

return Promise.all(promises):thenCall(function(resolvedValues)
return Promise.all(promises):and_then(function(resolvedValues)
local settledTable = {}
for i, key in ipairs(keys) do
settledTable[key] = resolvedValues[i]
Expand All @@ -43,7 +43,7 @@ return function(hashTable)
remaining = remaining + 1

settleNestedPromises(promise)
:thenCall(function(resolvedValue)
:and_then(function(resolvedValue)
if resolvedValue.status == "fulfilled" or resolvedValue.status == "rejected" then
results[key] = resolvedValue
else
Expand Down
3 changes: 1 addition & 2 deletions lua/promise/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,8 @@ function Promise.await(promise)
errored = true
end

promise:thenCall(handleResolution, handleRejection)
promise:and_then(handleResolution, handleRejection)

-- Use vim.wait to periodically check the promise state
while not resolved and not errored do
vim.wait(10, function()
-- Return false to continue waiting
Expand Down
26 changes: 13 additions & 13 deletions lua/promise/new.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ local function adopt_promise_state(promise, x, resolve, reject)
end

if x and (type(x) == "table" or is_callable(x)) then
local success, thenCall
success, thenCall = pcall(function()
return x.thenCall
local success, and_then
success, and_then = pcall(function()
return x.and_then
end)

if success and is_callable(thenCall) then
if success and is_callable(and_then) then
local called = false

local function resolve_once(y)
Expand All @@ -52,7 +52,7 @@ local function adopt_promise_state(promise, x, resolve, reject)
end

local success_inner, err = pcall(function()
thenCall(x, resolve_once, reject_once)
and_then(x, resolve_once, reject_once)
end)

if not success_inner then
Expand All @@ -62,7 +62,7 @@ local function adopt_promise_state(promise, x, resolve, reject)
if success then
resolve(x)
else
reject(thenCall)
reject(and_then)
end
end
else
Expand All @@ -79,7 +79,7 @@ return function(Promise, executor)
_state = "pending", -- 'pending', 'fulfilled', 'rejected'
_value = nil,
_reason = nil,
_thenCallbacks = {},
_and_thenbacks = {},
_finallyCallbacks = {},
_triggerUnhandledRejection = function(self, reason)
if not self._caught then
Expand All @@ -101,7 +101,7 @@ return function(Promise, executor)

if newState == "fulfilled" then
promise._value = result
for _, callbackPair in ipairs(promise._thenCallbacks) do
for _, callbackPair in ipairs(promise._and_thenbacks) do
if callbackPair[1] then
defer(Promise, function()
callbackPair[1](result)
Expand All @@ -110,7 +110,7 @@ return function(Promise, executor)
end
elseif newState == "rejected" then
promise._reason = result
for _, callbackPair in ipairs(promise._thenCallbacks) do
for _, callbackPair in ipairs(promise._and_thenbacks) do
if callbackPair[2] then
defer(Promise, function()
callbackPair[2](result)
Expand All @@ -124,7 +124,7 @@ return function(Promise, executor)
end

-- Clear callbacks after execution
promise._thenCallbacks = nil
promise._and_thenbacks = nil
promise._finallyCallbacks = nil
end

Expand Down Expand Up @@ -163,7 +163,7 @@ return function(Promise, executor)
end)
coroutine.resume(co)

function promise:thenCall(onFulfilled, onRejected)
function promise:and_then(onFulfilled, onRejected)
self._caught = true
local nextPromise
nextPromise = Promise:new(function(resolve, reject)
Expand Down Expand Up @@ -191,7 +191,7 @@ return function(Promise, executor)
elseif self._state == "rejected" then
handleCallback(onRejected, self._reason, resolve, reject)
elseif self._state == "pending" then
table.insert(self._thenCallbacks, {
table.insert(self._and_thenbacks, {
function(value)
handleCallback(onFulfilled, value, resolve, reject)
end,
Expand All @@ -208,7 +208,7 @@ return function(Promise, executor)
function promise:catch(onRejected)
self._caught = true

return self:thenCall(nil, onRejected)
return self:and_then(nil, onRejected)
end

function promise:finally(onFinally)
Expand Down
4 changes: 2 additions & 2 deletions lua/promise/parallel.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ return function(tasks)
for i, task in ipairs(tasks) do
local result = task()

if type(result) ~= "table" or type(result.thenCall) ~= "function" then
if type(result) ~= "table" or type(result.and_then) ~= "function" then
return reject("Promise.parallel: all functions must return a promise")
end

result
:thenCall(function(value)
:and_then(function(value)
if has_rejected then
return
end
Expand Down
12 changes: 6 additions & 6 deletions lua/promise/race.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,29 @@ return function(promises)

return Promise:new(function(resolve, reject)
local function handleResolution(value)
if type(value) == "table" and type(value.thenCall) == "function" then
if type(value) == "table" and type(value.and_then) == "function" then
-- If the resolved value is a promise, continue racing with it
value:thenCall(handleResolution, reject)
value:and_then(handleResolution, reject)
else
-- Otherwise, resolve with the value
resolve(value)
end
end

local function handleRejection(reason)
if type(reason) == "table" and type(reason.thenCall) == "function" then
if type(reason) == "table" and type(reason.and_then) == "function" then
-- If the rejection reason is a promise, continue racing with it
reason:thenCall(resolve, handleRejection)
reason:and_then(resolve, handleRejection)
else
-- Otherwise, reject with the reason
reject(reason)
end
end

for _, item in ipairs(promises) do
if type(item) == "table" and type(item.thenCall) == "function" then
if type(item) == "table" and type(item.and_then) == "function" then
-- Handle promise-like objects
item:thenCall(handleResolution, handleRejection)
item:and_then(handleResolution, handleRejection)
else
-- Resolve immediately if it's not a promise
resolve(item)
Expand Down
4 changes: 2 additions & 2 deletions lua/promise/series.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ return function(inputs)
return
end

if type(promise) ~= "table" or type(promise.thenCall) ~= "function" then
if type(promise) ~= "table" or type(promise.and_then) ~= "function" then
reject("Promise.series: all functions must return a promise")
return
end

promise
:thenCall(function(result)
:and_then(function(result)
table.insert(results, result)
next() -- Proceed to the next function in the series
end)
Expand Down
2 changes: 1 addition & 1 deletion lua/promise/try_each.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ return function(promises)
index = index + 1

currentPromise
:thenCall(function(value)
:and_then(function(value)
-- Resolve as soon as the first promise resolves
resolve(value)
end)
Expand Down
4 changes: 2 additions & 2 deletions lua/promise/waterfall.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ return function(tasks)
if not status then
reject(result)
else
if type(result) == "table" and type(result.thenCall) == "function" then
if type(result) == "table" and type(result.and_then) == "function" then
result
:thenCall(function(nextResult)
:and_then(function(nextResult)
next(index + 1, nextResult)
end)
:catch(function(err)
Expand Down
Loading

0 comments on commit 1395e08

Please sign in to comment.