Skip to content

Commit 1cb8453

Browse files
committed
Make sure to await guard functions in defgenAsync
1 parent 17b663a commit 1cb8453

2 files changed

Lines changed: 35 additions & 4 deletions

File tree

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "tailored",
3-
"version": "2.4.1",
3+
"version": "2.4.2",
44
"description": "Pattern matching library",
55
"main": "lib/tailored.js",
66
"jsnext:main": "src/index.js",
@@ -29,4 +29,4 @@
2929
"dependencies": {
3030
"erlang-types": "^1.0.0"
3131
}
32-
}
32+
}

src/tailored/defmatch.js

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,39 @@ export function defmatchAsync(...clauses) {
7171
const arities = getArityMap(clauses);
7272

7373
return async function(...args) {
74-
let [funcToCall, params] = findMatchingFunction(args, arities);
75-
return funcToCall.apply(this, params);
74+
if (arities.has(args.length)) {
75+
const arityClauses = arities.get(args.length);
76+
77+
let funcToCall = null;
78+
let params = null;
79+
for (let processedClause of arityClauses) {
80+
let result = [];
81+
args = fillInOptionalValues(
82+
args,
83+
processedClause.arity,
84+
processedClause.optionals,
85+
);
86+
87+
if (
88+
processedClause.pattern(args, result) &&
89+
(await processedClause.guard.apply(this, result))
90+
) {
91+
funcToCall = processedClause.fn;
92+
params = result;
93+
break;
94+
}
95+
}
96+
97+
if (!funcToCall) {
98+
console.error('No match for:', args);
99+
throw new MatchError(args);
100+
}
101+
102+
return funcToCall.apply(this, params);
103+
} else {
104+
console.error('Arity of', args.length, 'not found. No match for:', args);
105+
throw new MatchError(args);
106+
}
76107
};
77108
}
78109

0 commit comments

Comments
 (0)