Skip to content

Commit

Permalink
docs(examples): improved error handling in examples using Promise.all
Browse files Browse the repository at this point in the history
* docs(examples): improved error handling in examples using Promise.all
* docs(examples): improved map example
  • Loading branch information
mgordel authored May 10, 2024
1 parent b4d2e89 commit ce74ea8
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
10 changes: 8 additions & 2 deletions examples/docs-examples/examples/executing-tasks/map.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,14 @@ import { TaskExecutor, pinoPrettyLogger } from "@golem-sdk/task-executor";
}),
);

const results = await Promise.all(futureResults);
results.forEach((result) => console.log(result.stdout));
const results = await Promise.allSettled(futureResults);
results.forEach((result) => {
if (result.status === "fulfilled") {
console.log("Success", result.value.stdout);
} else {
console.log("Failure", result.value.reason);
}
});
} catch (err) {
console.error("An error occurred:", err);
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,14 @@ import { TaskExecutor, pinoPrettyLogger } from "@golem-sdk/task-executor";
try {
const data = [1, 2, 3, 4, 5];
const futureResults = data.map((item) => executor.run((ctx) => ctx.run(`echo "${item}"`)));
const results = await Promise.all(futureResults);
results.forEach((result) => console.log(result.stdout));
const results = await Promise.allSettled(futureResults);
results.forEach((result) => {
if (result.status === "fulfilled") {
console.log("Success", result.value.stdout);
} else {
console.log("Failure", result.value.reason);
}
});
} catch (err) {
console.error("Error occurred during task execution:", err);
} finally {
Expand Down
5 changes: 3 additions & 2 deletions examples/simple-usage/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ import { TaskExecutor, pinoPrettyLogger } from "@golem-sdk/task-executor";
}),
);

const results = await Promise.all(futureResults);
console.log(results);
const results = await Promise.allSettled(futureResults);
const successResults = results.filter((res) => res.status === "fulfilled");
console.log("Results:", successResults);
} catch (err) {
console.error("An error occurred:", err);
} finally {
Expand Down

0 comments on commit ce74ea8

Please sign in to comment.