mirror of
https://github.com/leporello-js/leporello-js
synced 2026-01-13 13:04:30 -08:00
WIP
This commit is contained in:
@@ -7,42 +7,32 @@ export const patch_promise = window => {
|
||||
|
||||
class PromiseWithStatus extends Promise {
|
||||
constructor(fn) {
|
||||
let status
|
||||
let is_constructor_finished = false
|
||||
super(
|
||||
(resolve, reject) => {
|
||||
fn(
|
||||
(value) => {
|
||||
if(value instanceof window.Promise.Original) {
|
||||
value
|
||||
.then(v => {
|
||||
this.status = {ok: true, value: v}
|
||||
resolve(v)
|
||||
})
|
||||
.catch(e => {
|
||||
this.status = {ok: false, error: e}
|
||||
reject(e)
|
||||
})
|
||||
} else {
|
||||
status = {ok: true, value}
|
||||
if(is_constructor_finished) {
|
||||
this.status = status
|
||||
}
|
||||
resolve(value)
|
||||
}
|
||||
},
|
||||
(error) => {
|
||||
status = {ok: false, error}
|
||||
if(is_constructor_finished) {
|
||||
this.status = status
|
||||
}
|
||||
reject(error)
|
||||
},
|
||||
)
|
||||
}
|
||||
)
|
||||
is_constructor_finished = true
|
||||
this.status = status
|
||||
const p = new Promise.Original((resolve, reject) => {
|
||||
fn(
|
||||
(value) => {
|
||||
if(value instanceof window.Promise) {
|
||||
value
|
||||
.then(v => {
|
||||
p.status = {ok: true, value: v}
|
||||
resolve(v)
|
||||
})
|
||||
.catch(e => {
|
||||
p.status = {ok: false, error: e}
|
||||
reject(e)
|
||||
})
|
||||
} else {
|
||||
p.status = {ok: true, value}
|
||||
resolve(value)
|
||||
}
|
||||
},
|
||||
(error) => {
|
||||
p.status = {ok: false, error}
|
||||
reject(error)
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
return p
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
23
test/test.js
23
test/test.js
@@ -2612,6 +2612,15 @@ const y = x()`
|
||||
)
|
||||
}),
|
||||
|
||||
test('async/await await Promise resolved immediately', async () => {
|
||||
await assert_code_evals_to_async(
|
||||
`
|
||||
await new Promise(resolve => resolve(1))
|
||||
`,
|
||||
1
|
||||
)
|
||||
}),
|
||||
|
||||
test('async/await return from async function', async () => {
|
||||
await assert_code_evals_to_async(
|
||||
`
|
||||
@@ -2746,6 +2755,20 @@ const y = x()`
|
||||
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 () => {
|
||||
const i = await test_initial_state_async(`
|
||||
// Init promises p1 and p2 that are resolved in different order (p2 then
|
||||
|
||||
Reference in New Issue
Block a user