Skip to content

Commit cf20fef

Browse files
committed
initial commit, no substantial code changes
1 parent 9ba7d86 commit cf20fef

File tree

4 files changed

+69
-47
lines changed

4 files changed

+69
-47
lines changed

Promise.lua

+23-7
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,25 @@
1616
-- promise:andThen(onFulfilled1):andThen(onFulfilled2, onRejected2)
1717
--
1818
-- History:
19-
-- 2015.10.29 release v1.1, fix some bugs and update testcases
20-
-- 2015.08.10 release v1.0.1, full testcases, minor fix and publish on github
19+
-- 2015.10.29 release v1.1, fix some bugs and update test cases
20+
-- 2015.08.10 release v1.0.1, full test cases, minor fix and publish on github
2121
-- 2015.03 release v1.0.0
2222
-----------------------------------------------------------------------------
2323

24-
local Promise, promise = {}, {}
24+
local Promise = {}
25+
26+
---@class Promise
27+
local promise = {}
2528

2629
-- andThen replacer
2730
-- 1) replace standard .then() when promised
2831
local PENDING = {}
2932
local nil_promise = {}
3033

34+
3135
local function promised(value, action)
3236
local ok, result = pcall(action, value)
37+
--print("Called handler with " .. tostring(value) .. " received " .. tostring(ok) .. " + " .. tostring(value))
3338
return ok and Promise.resolve(result) or Promise.reject(result) -- .. '.\n' .. debug.traceback())
3439
end
3540

@@ -45,7 +50,7 @@ local function promised_n(self, _, onRejected)
4550
return onRejected and promised(self[1], onRejected) or self
4651
end
4752

48-
-- inext() list all elementys in array
53+
-- inext() list all elements in array
4954
-- *) next() will list all members for table without order
5055
-- *) @see iter(): http://www.lua.org/pil/7.3.html
5156
local function inext(a, i)
@@ -55,10 +60,11 @@ local function inext(a, i)
5560
end
5661
-- put resolved value to p[1], or push lazyed calls/object to p[]
5762
-- 1) if resolved a no pending promise, direct call promise.andThen()
63+
---@param resolved Promise
5864
local function resolver(this, resolved, sure)
5965
local typ = type(resolved)
6066
if (typ == 'table' and resolved.andThen) then
61-
local lazy = {this,
67+
local lazy = --[[---@type Promise ]] {this,
6268
function(value) return resolver(this, value, true) end,
6369
function(reason) return resolver(this, reason, false) end}
6470
if resolved[1] == PENDING then
@@ -119,12 +125,22 @@ setmetatable(nil_promise, promise)
119125
-- 2) promise:catch(onRejected)
120126
------------------------------------------------------------------------------------------
121127
128+
-- Creates a new promise instance in the pending state
129+
-- from the fulfill and the reject functions.
130+
131+
---@param onFulfilled function | nil
132+
---@param onRejected function | nil
133+
---@return Promise
122134
function promise:andThen(onFulfilled, onRejected)
123-
local lazy = {{PENDING}, onFulfilled, onRejected}
124-
table.insert(self, lazy)
135+
local lazy = --[[---@type Promise ]] {{PENDING}, onFulfilled, onRejected}
136+
table.insert(--[[---@type Promise[] ]] self, lazy)
125137
return setmetatable(lazy[1], promise) -- <lazy[1]> is promise2
126138
end
127139
140+
-- Creates a new promise instance in the pending state
141+
-- from just the reject function.
142+
---@param onRejected function
143+
---@return Promise
128144
function promise:catch(onRejected)
129145
return self:andThen(nil, onRejected)
130146
end

README.md

+11-11
Original file line numberDiff line numberDiff line change
@@ -100,31 +100,31 @@ This is a base testcase:
100100
---
101101
Promise = require('Promise')
102102
103-
A = function() return 10 end
104-
B = function(a) print(a * 2) end
105-
C = function(a)
103+
return10 = function() return 10 end
104+
returnDoubled = function(a) print(a * 2) end
105+
printX4 = function(a)
106106
print(a * 4)
107107
return Promise.resolve('ok') -- or direct return 'ok'
108108
end
109-
D = function(a) print(a * 3) end
110-
E = function(result)
109+
printX3 = function(a) print(a * 3) end
110+
unpackAndReject = function(result)
111111
local b, c, d = unpack(result)
112112
print(b, c, d)
113113
return Promise.reject('FIRE')
114114
end
115115
116116
-- promise_A = Promise.resolve(A())
117-
promise_A = Promise.new(function(resolve, reject)
118-
local ok, result = pcall(A)
117+
promise_r10 = Promise.new(function(resolve, reject)
118+
local ok, result = pcall(return10)
119119
return (ok and resolve or reject)(result)
120120
end)
121-
promise_B = promise_A:andThen(B)
122-
promise_C = promise_A:andThen(C)
123-
promise_D = promise_A:andThen(D)
121+
promise_B = promise_r10:andThen(returnDoubled)
122+
promise_C = promise_r10:andThen(printX4)
123+
promise_D = promise_r10:andThen(printX3)
124124
125125
promises = {promise_B, promise_C, promise_D}
126126
Promise.all(promises)
127-
:andThen(E)
127+
:andThen(unpackAndReject)
128128
:catch(function(reson)
129129
print(reson)
130130
end)

testcase/t_base_test.lua

+22-16
Original file line numberDiff line numberDiff line change
@@ -12,37 +12,43 @@
1212
-- FIRE
1313
---------------------------------------------------------------
1414

15-
-- Promise = require('Promise')
16-
Promise = dofile('../Promise.lua')
15+
local inspect = require "IO/inspect"
1716

18-
A = function() return 10 end
19-
B = function(a) print(a * 2) end
20-
C = function(a)
17+
Promise = require('Promise')
18+
--Promise = require("promises/Promise/Promise")
19+
print("---")
20+
return10 = function() return 10 end
21+
returnDoubled = function(a) print(a * 2) ; return a * 2 end
22+
printX4 = function(a)
2123
print(a * 4)
22-
-- return Promise.resolve('ok')
23-
return 'ok'
24+
return Promise.resolve('ok')
25+
-- return 'ok'
2426
end
25-
D = function(a) print(a * 3) end
26-
E = function(result)
27+
printX3 = function(a) print(a * 3) ; return a * 3 end
28+
unpackAndReject = function(result)
2729
local b, c, d = unpack(result)
2830
print(b, c, d)
2931
return Promise.reject('FIRE')
3032
end
3133

3234
-- promise_A = Promise.resolve(A())
33-
promise_A = Promise.new(function(resolve, reject)
34-
local ok, result = pcall(A)
35+
promise_r10 = Promise.new(function(resolve, reject)
36+
local ok, result = pcall(return10)
37+
print("after pcall " .. tostring(ok) .. " " .. result .. ".")
3538
return (ok and resolve or reject)(result)
3639
end)
40+
promise_r10 = Promise.resolve(10)
3741
-- promise_B = promise_A:andThen(B)
3842
local err = function(r) print("catch:", r) end
3943
local log = function(r) print("andThen:", r); return r end
40-
promise_B = promise_A:andThen(B):catch(err):andThen(log)
44+
promise_B = promise_r10:andThen(returnDoubled):catch(err):andThen(log)
4145

42-
promise_C = promise_A:andThen(C)
43-
promise_D = promise_A:andThen(D)
46+
promise_C = promise_r10:andThen(printX4)
47+
promise_D = promise_r10:andThen(printX3)
4448

49+
50+
print(inspect({"hi"}))
4551
promises = {promise_B, promise_C, promise_D}
46-
Promise.all(promises)
47-
:andThen(E)
52+
Promise.all(promises):andThen(function(o) print(inspect(o)) return o end)
53+
:andThen(unpackAndReject)
4854
:catch(print)

testcase/t_rpc_test.lua

+13-13
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717
---------------------------------------------------------------
1818

1919
-- Promise = require('Promise')
20-
Promise = dofile('../Promise.lua')
20+
Promise = dofile('Promise.lua')
2121
sleep = function(n) os.execute("sleep " .. n) end
2222

2323
local co_remote
24-
A = function() return 10 end
25-
B = function(a)
24+
return10 = function() return 10 end
25+
returnDoubled = function(a)
2626
return Promise.new(function(...)
2727
-- start coroutine and wait
2828
co_remote = coroutine.create(function(y, n)
@@ -32,32 +32,32 @@ B = function(a)
3232
coroutine.resume(co_remote, ...)
3333
end)
3434
end
35-
C = function(a)
35+
printX4 = function(a)
3636
print(a * 4)
3737
return Promise.resolve('ok')
3838
end
39-
D = function(a) print(a * 3) end
40-
E = function(result)
41-
local b, c, d = unpack(result)
39+
printX3 = function(a) print(a * 3) end
40+
unpackAndReject = function(...)
41+
local b, c, d = ...
4242
print(b, c, d)
4343
return Promise.reject('FIRE')
4444
end
4545

4646
-- promise_A = Promise.resolve(A())
47-
promise_A = Promise.new(function(resolve, reject)
48-
local ok, result = pcall(A)
47+
promise_r10 = Promise.new(function(resolve, reject)
48+
local ok, result = pcall(return10)
4949
return (ok and resolve or reject)(result)
5050
end)
5151

5252
local err = function(r) print("catch:", r) end
5353
local log = function(r) print("andThen:", r); return r end
54-
promise_B = promise_A:andThen(B):catch(err):andThen(log)
55-
promise_C = promise_A:andThen(C)
56-
promise_D = promise_A:andThen(D)
54+
promise_B = promise_r10:andThen(returnDoubled):catch(err):andThen(log)
55+
promise_C = promise_r10:andThen(printX4)
56+
promise_D = promise_r10:andThen(printX3)
5757

5858
promises = {promise_B, promise_C, promise_D}
5959
Promise.all(promises)
60-
:andThen(E)
60+
:andThen(unpackAndReject)
6161
:catch(function(reson)
6262
print(reson)
6363
end)

0 commit comments

Comments
 (0)