@@ -37,8 +37,16 @@ const drinkRule = {
3737 } ,
3838 event : { type : 'drinks-screwdrivers' } ,
3939 priority : 10 , // IMPORTANT! Set a higher priority for the drinkRule, so it runs first
40- onSuccess : function ( event , almanac ) {
40+ onSuccess : async function ( event , almanac ) {
4141 almanac . addRuntimeFact ( 'screwdriverAficionado' , true )
42+
43+ // asychronous operations can be performed within callbacks
44+ // execution will not proceed until returned promises are resolved
45+ const zipCode = await almanac . factValue ( 'zipCode' )
46+ if ( zipCode > 80014 && zipCode < 80642 ) {
47+ const hometown = 'Denver'
48+ almanac . addRuntimeFact ( 'hometown' , hometown ) // overrides default 'hometown' fact value
49+ }
4250 } ,
4351 onFailure : function ( event , almanac ) {
4452 almanac . addRuntimeFact ( 'screwdriverAficionado' , false )
@@ -61,6 +69,10 @@ const inviteRule = {
6169 fact : 'isSociable' ,
6270 operator : 'equal' ,
6371 value : true
72+ } , {
73+ fact : 'hometown' ,
74+ operator : 'equal' ,
75+ value : 'Denver'
6476 } ]
6577 } ,
6678 event : { type : 'invite-to-screwdriver-social' } ,
8193 } )
8294
8395// define fact(s) known at runtime
84- facts = { accountId : 'washington' , drinksOrangeJuice : true , enjoysVodka : true , isSociable : true }
96+ facts = { accountId : 'washington' , drinksOrangeJuice : true , enjoysVodka : true , isSociable : true , zipCode : 80211 , hometown : 'unknown' }
8597engine
8698 . run ( facts ) // first run, using washington's facts
8799 . then ( ( results ) => {
@@ -90,10 +102,10 @@ engine
90102 return results . almanac . factValue ( 'screwdriverAficionado' )
91103 } )
92104 . then ( isScrewdriverAficionado => {
93- console . log ( `${ facts . accountId } ${ isScrewdriverAficionado ? 'IS' . green : 'IS NOT' . red } a screwdriver aficionado` )
105+ console . log ( `${ facts . accountId } ${ isScrewdriverAficionado ? 'IS' . green : 'IS NOT' . red } a screwdriver aficionado in Denver ` )
94106 } )
95107 . then ( ( ) => {
96- facts = { accountId : 'jefferson' , drinksOrangeJuice : true , enjoysVodka : false , isSociable : true }
108+ facts = { accountId : 'jefferson' , drinksOrangeJuice : true , enjoysVodka : false , isSociable : true , zipCode : 80248 , hometown : 'unknown' }
97109 return engine . run ( facts ) // second run, using jefferson's facts; facts & evaluation are independent of the first run
98110 } )
99111 . then ( ( results ) => {
@@ -102,7 +114,7 @@ engine
102114 return results . almanac . factValue ( 'screwdriverAficionado' )
103115 } )
104116 . then ( isScrewdriverAficionado => {
105- console . log ( `${ facts . accountId } ${ isScrewdriverAficionado ? 'IS' . green : 'IS NOT' . red } a screwdriver aficionado` )
117+ console . log ( `${ facts . accountId } ${ isScrewdriverAficionado ? 'IS' . green : 'IS NOT' . red } a screwdriver aficionado in Denver ` )
106118 } )
107119 . catch ( console . log )
108120
@@ -111,8 +123,8 @@ engine
111123 *
112124 * washington DID meet conditions for the drinks-screwdrivers rule.
113125 * washington DID meet conditions for the invite-to-screwdriver-social rule.
114- * washington IS a screwdriver aficionado
126+ * washington IS a screwdriver aficionado in Denver
115127 * jefferson did NOT meet conditions for the drinks-screwdrivers rule.
116128 * jefferson did NOT meet conditions for the invite-to-screwdriver-social rule.
117- * jefferson IS NOT a screwdriver aficionado
129+ * jefferson IS NOT a screwdriver aficionado in Denver
118130 */
0 commit comments