This commit is contained in:
Dmitry Vasilev
2022-12-16 21:43:07 +08:00
parent 416fdcf20b
commit 769ca707eb
2 changed files with 51 additions and 60 deletions

View File

@@ -1,5 +1,5 @@
export const patch_promise = window => { export const patch_promise = window => {
if(window.Promise.Original != null) { if(window.Promise.Original != null) {
// already patched // already patched
return return
@@ -7,31 +7,42 @@ 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
fn( let is_constructor_finished = false
(value) => { const p = new Promise.Original(
if(value instanceof window.Promise) { (resolve, reject) => {
value fn(
.then(v => { (value) => {
p.status = {ok: true, value: v} if(value instanceof window.Promise.Original) {
resolve(v) value
}) .then(v => {
.catch(e => { p.status = {ok: true, value: v}
p.status = {ok: false, error: e} resolve(v)
reject(e) })
}) .catch(e => {
} else { p.status = {ok: false, error: e}
p.status = {ok: true, value} reject(e)
resolve(value) })
} } else {
}, status = {ok: true, value}
(error) => { if(is_constructor_finished) {
p.status = {ok: false, error} p.status = status
reject(error) }
}, resolve(value)
) }
}) },
(error) => {
status = {ok: false, error}
if(is_constructor_finished) {
p.status = status
}
reject(error)
},
)
}
)
is_constructor_finished = true
p.status = status
return p return p
} }
} }

View File

@@ -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])
}),
] ]