mirror of
https://github.com/leporello-js/leporello-js
synced 2026-01-13 21:14:28 -08:00
fix
This commit is contained in:
@@ -7,10 +7,13 @@ export const patch_promise = window => {
|
|||||||
|
|
||||||
class PromiseWithStatus extends Promise {
|
class PromiseWithStatus extends Promise {
|
||||||
constructor(fn) {
|
constructor(fn) {
|
||||||
const p = new Promise.Original((resolve, reject) => {
|
let status
|
||||||
|
let is_constructor_finished = false
|
||||||
|
const p = new Promise.Original(
|
||||||
|
(resolve, reject) => {
|
||||||
fn(
|
fn(
|
||||||
(value) => {
|
(value) => {
|
||||||
if(value instanceof window.Promise) {
|
if(value instanceof window.Promise.Original) {
|
||||||
value
|
value
|
||||||
.then(v => {
|
.then(v => {
|
||||||
p.status = {ok: true, value: v}
|
p.status = {ok: true, value: v}
|
||||||
@@ -21,17 +24,25 @@ export const patch_promise = window => {
|
|||||||
reject(e)
|
reject(e)
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
p.status = {ok: true, value}
|
status = {ok: true, value}
|
||||||
|
if(is_constructor_finished) {
|
||||||
|
p.status = status
|
||||||
|
}
|
||||||
resolve(value)
|
resolve(value)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
(error) => {
|
(error) => {
|
||||||
p.status = {ok: false, error}
|
status = {ok: false, error}
|
||||||
|
if(is_constructor_finished) {
|
||||||
|
p.status = status
|
||||||
|
}
|
||||||
reject(error)
|
reject(error)
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
})
|
}
|
||||||
|
)
|
||||||
|
is_constructor_finished = true
|
||||||
|
p.status = status
|
||||||
return p
|
return p
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
48
test/test.js
48
test/test.js
@@ -2724,15 +2724,10 @@ const y = x()`
|
|||||||
const i = await test_initial_state_async(`
|
const i = await test_initial_state_async(`
|
||||||
const x = () => 1
|
const x = () => 1
|
||||||
const delay = async time => {
|
const delay = async time => {
|
||||||
await 1 //TODO Promise.resolve()
|
await 1
|
||||||
x()
|
x()
|
||||||
}
|
}
|
||||||
await delay(3)
|
await delay(3)
|
||||||
/* TODO
|
|
||||||
await Promise.all([
|
|
||||||
delay(3),
|
|
||||||
])
|
|
||||||
*/
|
|
||||||
`)
|
`)
|
||||||
const root = root_calltree_node(i)
|
const root = root_calltree_node(i)
|
||||||
assert_equal(root.children.length, 1)
|
assert_equal(root.children.length, 1)
|
||||||
@@ -2741,34 +2736,6 @@ const y = x()`
|
|||||||
assert_equal(call_delay.fn.name, 'delay')
|
assert_equal(call_delay.fn.name, 'delay')
|
||||||
}),
|
}),
|
||||||
|
|
||||||
// TODO
|
|
||||||
test('async/await logs out of order', async () => {
|
|
||||||
const i = await test_initial_state_async(`
|
|
||||||
const delay = async time => {
|
|
||||||
await new Promise(res => globalThis.setTimeout(res, time*10))
|
|
||||||
console.log(time)
|
|
||||||
}
|
|
||||||
|
|
||||||
await Promise.all([delay(2), delay(1)])
|
|
||||||
`)
|
|
||||||
const logs = i.logs.logs.map(l => l.args[0])
|
|
||||||
assert_equal(logs, [1, 2])
|
|
||||||
}),
|
|
||||||
|
|
||||||
|
|
||||||
/* TODO
|
|
||||||
test('p', async () => {
|
|
||||||
const i = await assert_code_evals_to_async(`
|
|
||||||
const res = Promise.resolve(1)
|
|
||||||
Object.assign(res, {mark: 'resolved'})
|
|
||||||
await Object.assign(new Promise(resolve => resolve()), {mark: 'w'})
|
|
||||||
`,
|
|
||||||
1
|
|
||||||
)
|
|
||||||
|
|
||||||
}),
|
|
||||||
*/
|
|
||||||
|
|
||||||
test('async/await logs out of order', async () => {
|
test('async/await logs out of order', async () => {
|
||||||
const i = await test_initial_state_async(`
|
const i = await test_initial_state_async(`
|
||||||
// Init promises p1 and p2 that are resolved in different order (p2 then
|
// Init promises p1 and p2 that are resolved in different order (p2 then
|
||||||
@@ -2787,4 +2754,17 @@ const y = x()`
|
|||||||
assert_equal(logs, [2, 1])
|
assert_equal(logs, [2, 1])
|
||||||
}),
|
}),
|
||||||
|
|
||||||
|
test('async/await logs out of order', async () => {
|
||||||
|
const i = await test_initial_state_async(`
|
||||||
|
const delay = async time => {
|
||||||
|
await new Promise(res => globalThis.setTimeout(res, time*10))
|
||||||
|
console.log(time)
|
||||||
|
}
|
||||||
|
|
||||||
|
await Promise.all([delay(2), delay(1)])
|
||||||
|
`)
|
||||||
|
const logs = i.logs.logs.map(l => l.args[0])
|
||||||
|
assert_equal(logs, [1, 2])
|
||||||
|
}),
|
||||||
|
|
||||||
]
|
]
|
||||||
|
|||||||
Reference in New Issue
Block a user