#Promise-propagation

7 messages · Page 1 of 1 (latest)

still vapor
#

hi all, thanks everyone for your help these last couple of days! I've got everything working with the typescript errors ignored 🎉

now just trying to solve a few last remaining typescript errors.. this code relates to building functions that can work with promises from callbacks - but if the callbacks do not return promises, then the function does not return a promise

#

with examples:

severe merlinBOT
#
m.a.t.t.t#0

Preview:```ts
type MaybePromise<T> = T | Promise<T>

function wait<V, X>(
value: V,
resolver: (value: Awaited<V>) => X
): MaybePromise<X> {
return value instanceof Promise
? value.then(resolver)
: resolver(value)
}

function waitMap<V, X>(
values: V[],
mapper: (value: V) => X
): MaybePromis
...```

ancient vale
#

in waitMap is the intention that values is either an array of all promises or none? or can it be a mix, with only some elements being promises? also for the return type: are you sure you meant MaybePromise<X[]> and not MaybePromise<X>[]?

#

assuming you actually meant MaybePromise<X>[], here's an attempt:

severe merlinBOT
#
mkantor#0

Preview:```ts
...
function wait<V, X>(
value: MaybePromise<V>,
resolver: (value: V) => X
): MaybePromise<X> {
return value instanceof Promise
? value.then(resolver)
: resolver(value)
}

function waitMap<V, X>(
values: MaybePromise<V>[],
mapper: (value: V) => X
): MaybePromise<X>[] {
return values.reduce(
(acc: MaybePromise<X>[], value) => [
...acc,
wait(value, mapper),
],
[]
)
}
...```

still vapor
#

ah brilliant, thanks for cleaning up wait that looks much better!

for waitMap I had a logic bug but now it should be working properly, with each value in the array resolved serially rather than in parallel