@nodescript/stdlib
Version:
Standard Node Definitions
37 lines (36 loc) • 867 B
JavaScript
export const module = {
version: '1.2.0',
moduleName: 'Flow / Try',
description: `Runs the steps one-by-one and returns the first result that doesn't throw an error.`,
params: {
steps: {
deferred: true,
schema: {
type: 'array',
items: { type: 'any' },
},
},
},
result: {
async: true,
schema: { type: 'any' },
}
};
export const compute = async (params, ctx) => {
let lastError = null;
for (const step of params.steps) {
try {
return await ctx.resolveDeferred(step);
}
catch (error) {
if (error.code === 'EPENDING') {
throw error;
}
lastError = error;
}
}
if (lastError) {
throw lastError;
}
return undefined;
};