@@ -131,39 +131,43 @@ class Rule extends EventEmitter {
131131 * All evaluation is done within the context of an almanac
132132 * @return {Promise(RuleResult) } rule evaluation result
133133 */
134- async evaluate ( almanac ) {
134+ evaluate ( almanac ) {
135135 let ruleResult = new RuleResult ( this . conditions , this . event , this . priority )
136136
137137 /**
138138 * Evaluates the rule conditions
139139 * @param {Condition } condition - condition to evaluate
140140 * @return {Promise(true|false) } - resolves with the result of the condition evaluation
141141 */
142- let evaluateCondition = async ( condition ) => {
143- let comparisonValue
144- let passes
142+ let evaluateCondition = ( condition ) => {
145143 if ( condition . isBooleanOperator ( ) ) {
146144 let subConditions = condition [ condition . operator ]
145+ let comparisonPromise
147146 if ( condition . operator === 'all' ) {
148- comparisonValue = await all ( subConditions )
147+ comparisonPromise = all ( subConditions )
149148 } else {
150- comparisonValue = await any ( subConditions )
149+ comparisonPromise = any ( subConditions )
151150 }
152151 // for booleans, rule passing is determined by the all/any result
153- passes = comparisonValue === true
152+ return comparisonPromise . then ( comparisonValue => {
153+ let passes = comparisonValue === true
154+ condition . result = passes
155+ return passes
156+ } )
154157 } else {
155- try {
156- let evaluationResult = await condition . evaluate ( almanac , this . engine . operators , comparisonValue )
157- passes = evaluationResult . result
158- condition . factResult = evaluationResult . leftHandSideValue
159- } catch ( err ) {
160- // any condition raising an undefined fact error is considered falsey when allowUndefinedFacts is enabled
161- if ( this . engine . allowUndefinedFacts && err . code === 'UNDEFINED_FACT' ) passes = false
162- else throw err
163- }
158+ return condition . evaluate ( almanac , this . engine . operators )
159+ . then ( evaluationResult => {
160+ let passes = evaluationResult . result
161+ condition . factResult = evaluationResult . leftHandSideValue
162+ condition . result = passes
163+ return passes
164+ } )
165+ . catch ( err => {
166+ // any condition raising an undefined fact error is considered falsey when allowUndefinedFacts is enabled
167+ if ( this . engine . allowUndefinedFacts && err . code === 'UNDEFINED_FACT' ) return false
168+ throw err
169+ } )
164170 }
165- condition . result = passes
166- return passes
167171 }
168172
169173 /**
@@ -172,13 +176,14 @@ class Rule extends EventEmitter {
172176 * @param {string(every|some) } array method to call for determining result
173177 * @return {Promise(boolean) } whether conditions evaluated truthy or falsey based on condition evaluation + method
174178 */
175- let evaluateConditions = async ( conditions , method ) => {
179+ let evaluateConditions = ( conditions , method ) => {
176180 if ( ! ( Array . isArray ( conditions ) ) ) conditions = [ conditions ]
177- let conditionResults = await Promise . all ( conditions . map ( ( condition ) => {
178- return evaluateCondition ( condition )
179- } ) )
180- debug ( `rule::evaluateConditions results` , conditionResults )
181- return method . call ( conditionResults , ( result ) => result === true )
181+
182+ return Promise . all ( conditions . map ( ( condition ) => evaluateCondition ( condition ) ) )
183+ . then ( conditionResults => {
184+ debug ( `rule::evaluateConditions results` , conditionResults )
185+ return method . call ( conditionResults , ( result ) => result === true )
186+ } )
182187 }
183188
184189 /**
@@ -191,9 +196,9 @@ class Rule extends EventEmitter {
191196 * @param {string('all'|'any') } operator
192197 * @return {Promise(boolean) } rule evaluation result
193198 */
194- let prioritizeAndRun = async ( conditions , operator ) => {
199+ let prioritizeAndRun = ( conditions , operator ) => {
195200 if ( conditions . length === 0 ) {
196- return true
201+ return Promise . resolve ( true )
197202 }
198203 let method = Array . prototype . some
199204 if ( operator === 'all' ) {
@@ -229,7 +234,7 @@ class Rule extends EventEmitter {
229234 * @param {Condition[] } conditions to be evaluated
230235 * @return {Promise(boolean) } condition evaluation result
231236 */
232- let any = async ( conditions ) => {
237+ let any = ( conditions ) => {
233238 return prioritizeAndRun ( conditions , 'any' )
234239 }
235240
@@ -238,7 +243,7 @@ class Rule extends EventEmitter {
238243 * @param {Condition[] } conditions to be evaluated
239244 * @return {Promise(boolean) } condition evaluation result
240245 */
241- let all = async ( conditions ) => {
246+ let all = ( conditions ) => {
242247 return prioritizeAndRun ( conditions , 'all' )
243248 }
244249
@@ -255,11 +260,11 @@ class Rule extends EventEmitter {
255260 }
256261
257262 if ( ruleResult . conditions . any ) {
258- let result = await any ( ruleResult . conditions . any )
259- return processResult ( result )
263+ return any ( ruleResult . conditions . any )
264+ . then ( result => processResult ( result ) )
260265 } else {
261- let result = await all ( ruleResult . conditions . all )
262- return processResult ( result )
266+ return all ( ruleResult . conditions . all )
267+ . then ( result => processResult ( result ) )
263268 }
264269 }
265270}
0 commit comments