silverbullet/common/space_lua/util.ts

17 lines
489 B
TypeScript
Raw Normal View History

2024-09-12 23:01:54 +08:00
export function evalPromiseValues(vals: any[]): Promise<any[]> | any[] {
2024-09-30 18:50:54 +08:00
const promises = [];
const promiseResults = new Array(vals.length);
for (let i = 0; i < vals.length; i++) {
if (vals[i] instanceof Promise) {
promises.push(vals[i].then((v: any) => promiseResults[i] = v));
2024-09-12 23:01:54 +08:00
} else {
2024-09-30 18:50:54 +08:00
promiseResults[i] = vals[i];
2024-09-12 23:01:54 +08:00
}
2024-09-30 18:50:54 +08:00
}
if (promises.length === 0) {
return promiseResults;
} else {
return Promise.all(promises).then(() => promiseResults);
}
2024-09-12 23:01:54 +08:00
}