mirror of
https://github.com/leporello-js/leporello-js
synced 2026-01-13 13:04:30 -08:00
set status in Promise.then
This commit is contained in:
10
src/eval.js
10
src/eval.js
@@ -298,14 +298,18 @@ export const eval_modules = (
|
|||||||
promise_then = Promise.prototype.then
|
promise_then = Promise.prototype.then
|
||||||
|
|
||||||
Promise.prototype.then = function then(on_resolve, on_reject) {
|
Promise.prototype.then = function then(on_resolve, on_reject) {
|
||||||
|
|
||||||
if(children == null) {
|
if(children == null) {
|
||||||
children = []
|
children = []
|
||||||
}
|
}
|
||||||
let children_copy = children
|
let children_copy = children
|
||||||
|
|
||||||
const make_callback = cb => typeof(cb) != 'function'
|
const make_callback = (cb, ok) => typeof(cb) != 'function'
|
||||||
? cb
|
? cb
|
||||||
: value => {
|
: value => {
|
||||||
|
if(this.status == null) {
|
||||||
|
this.status = ok ? {ok, value} : {ok, error: value}
|
||||||
|
}
|
||||||
const current = children
|
const current = children
|
||||||
children = children_copy
|
children = children_copy
|
||||||
try {
|
try {
|
||||||
@@ -317,8 +321,8 @@ export const eval_modules = (
|
|||||||
|
|
||||||
return promise_then.call(
|
return promise_then.call(
|
||||||
this,
|
this,
|
||||||
make_callback(on_resolve),
|
make_callback(on_resolve, true),
|
||||||
make_callback(on_reject),
|
make_callback(on_reject, false),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
27
test/test.js
27
test/test.js
@@ -2749,6 +2749,33 @@ const y = x()`
|
|||||||
assert_equal(call_delay.fn.name, 'delay')
|
assert_equal(call_delay.fn.name, 'delay')
|
||||||
}),
|
}),
|
||||||
|
|
||||||
|
test('async/await Promise.all set child promises status ok', async () => {
|
||||||
|
const i = await test_initial_state_async(`
|
||||||
|
const async_fn = async () => 1
|
||||||
|
await Promise.all([1,2,3].map(async_fn))
|
||||||
|
`)
|
||||||
|
const async_fn_call =
|
||||||
|
root_calltree_node(i)
|
||||||
|
.children[0] // map
|
||||||
|
.children[0] // first call of async_fn
|
||||||
|
assert_equal(async_fn_call.value.status.ok, true)
|
||||||
|
assert_equal(async_fn_call.value.status.value, 1)
|
||||||
|
}),
|
||||||
|
|
||||||
|
test('async/await Promise.all set child promises status error',
|
||||||
|
async () => {
|
||||||
|
const i = await test_initial_state_async(`
|
||||||
|
const async_fn = async () => { throw 1 }
|
||||||
|
await Promise.all([1,2,3].map(async_fn))
|
||||||
|
`)
|
||||||
|
const async_fn_call =
|
||||||
|
root_calltree_node(i)
|
||||||
|
.children[0] // map
|
||||||
|
.children[0] // first call of async_fn
|
||||||
|
assert_equal(async_fn_call.value.status.ok, false)
|
||||||
|
assert_equal(async_fn_call.value.status.error, 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
|
||||||
|
|||||||
Reference in New Issue
Block a user