venus – 14-43 Oct 11

Hi is it possible to get rid of promise functions that throws error from promise.all array response? My solution kinda sucks cuz I am returning null on error and then removing it from array. Can you think of any better approach?
const serversPromise = servers.map(async ({ ip, port }) =>
getShortServerQuery(ip, port).catch(() => null)
);

// serversData: (Servertype | null)[]
let serversData = await Promise.all(serversPromise);

// My solution
serversData = serversData.filter((res) => res != null);
const serversPromise = servers.map(async ({ ip, port }) =>
getShortServerQuery(ip, port).catch(() => null)
);

// serversData: (Servertype | null)[]
let serversData = await Promise.all(serversPromise);

// My solution
serversData = serversData.filter((res) => res != null);
E
EvokeMe555d ago
async function query(n) {
if (n === 0) throw new Error(`Cannot multiple 0`)
return n * 2
}

async function main() {
const list = [1, 2, 3, 0, 5]
return Promise.allSettled(list.map(query))
}

const log = (v) => console.log(JSON.stringify(v)) // => [{"status":"fulfilled","value":2},{"status":"fulfilled","value":4},{"status":"fulfilled","value":6},{"status":"rejected","reason":{}},{"status":"fulfilled","value":10}]
main().then(log)
async function query(n) {
if (n === 0) throw new Error(`Cannot multiple 0`)
return n * 2
}

async function main() {
const list = [1, 2, 3, 0, 5]
return Promise.allSettled(list.map(query))
}

const log = (v) => console.log(JSON.stringify(v)) // => [{"status":"fulfilled","value":2},{"status":"fulfilled","value":4},{"status":"fulfilled","value":6},{"status":"rejected","reason":{}},{"status":"fulfilled","value":10}]
main().then(log)
@venus ^^
E
EvokeMe555d ago
UU
Unknown User555d ago