Using reactiveUtils.when inside Promise.all()

670
2
Jump to solution
08-19-2022 09:55 AM
JustinKirtz1
New Contributor III

I'm trying to convert my pre-4.24 code using watchUtils to now use reactiveUtils. I have a tool that lets a user place a point on the map, waits for four address point feature layers to load, waits for them to stop updating, then finds the closest address to the point they created. The code looks like this:

 

Promise.all([
                        appConfig.activeView.whenLayerView(greenvilleCountyAddresses),
                        appConfig.activeView.whenLayerView(andersonCountyAddresses),
                        appConfig.activeView.whenLayerView(pickensCountyAddresses),
                        appConfig.activeView.whenLayerView(laurensCountyAddresses),
                    ]).then(([layerView1, layerView2, layerView3, layerView4]) => {
                        return Promise.all(
                            [
                                watchUtils.whenFalseOnce(layerView1, "updating"),
                                watchUtils.whenFalseOnce(layerView2, "updating"),
                                watchUtils.whenFalseOnce(layerView3, "updating"),
                                watchUtils.whenFalseOnce(layerView4, "updating"),
                            ]
                        );
                    }).then(([layerView1, layerView2, layerView3, layerView4]) => {
                        addVehicleIncident_FeatureLayerView_GreenvilleCountyAddresses = layerView1.target;
                        addVehicleIncident_FeatureLayerView_AndersonCountyAddresses = layerView2.target;
                        addVehicleIncident_FeatureLayerView_PickensCountyAddresses = layerView3.target;
                        addVehicleIncident_FeatureLayerView_LaurensCountyAddresses = layerView4.target;
                        findClosestAddress_addVehicleIncident(addressSelectorBuffer, newAddVehicleIncidentFeature, greenvilleCountyAddresses, andersonCountyAddresses, pickensCountyAddresses, laurensCountyAddresses)
                    });

 

According to the docs PromisedWatchHandle is now deprecated so the Promise.all isn't working anymore when I try something like this:

Promise.all([
                        appConfig.activeView.whenLayerView(greenvilleCountyAddresses),
                        appConfig.activeView.whenLayerView(andersonCountyAddresses),
                        appConfig.activeView.whenLayerView(pickensCountyAddresses),
                        appConfig.activeView.whenLayerView(laurensCountyAddresses),
                    ]).then(([layerView1, layerView2, layerView3, layerView4]) => {
                        return Promise.all(
                            [
                                reactiveUtils.when(() => !layerView1.updating,() => {},{once: true}),
                                reactiveUtils.when(() => !layerView2.updating,() => {},{once: true}),
                                reactiveUtils.when(() => !layerView3.updating,() => {},{once: true}),
                                reactiveUtils.when(() => !layerView4.updating,() => {},{once: true}),
                            ]
                        );
                    }).then(([layerView1, layerView2, layerView3, layerView4]) => {
                        addVehicleIncident_FeatureLayerView_GreenvilleCountyAddresses = layerView1.target;
                        addVehicleIncident_FeatureLayerView_AndersonCountyAddresses = layerView2.target;
                        addVehicleIncident_FeatureLayerView_PickensCountyAddresses = layerView3.target;
                        addVehicleIncident_FeatureLayerView_LaurensCountyAddresses = layerView4.target;
                        findClosestAddress_addVehicleIncident(addressSelectorBuffer, newAddVehicleIncidentFeature, greenvilleCountyAddresses, andersonCountyAddresses, pickensCountyAddresses, laurensCountyAddresses)
                    });

I'm not the best with Promises so any help/advice would be greatly appreciated!

 

 

GIS Supervisor for Greenville Water
0 Kudos
1 Solution

Accepted Solutions
AndyGup
Esri Regular Contributor

@JustinKirtz1hmm, try replacing reactiveUtils.when() with whenOnce() since that returns a promise and works with truthy values, if you go that route you also won't need to set 'once: true'. In comparison, reactiveUtils.when() returns a WatchHandle. Check out the doc section on "WatchHandles and Promises" and also check the "Working with truthy values" section right below that.

View solution in original post

2 Replies
AndyGup
Esri Regular Contributor

@JustinKirtz1hmm, try replacing reactiveUtils.when() with whenOnce() since that returns a promise and works with truthy values, if you go that route you also won't need to set 'once: true'. In comparison, reactiveUtils.when() returns a WatchHandle. Check out the doc section on "WatchHandles and Promises" and also check the "Working with truthy values" section right below that.

JustinKirtz1
New Contributor III

Andy,

This worked, I just had to switch my code around a bit.

Promise.all([
                        appConfig.activeView.whenLayerView(greenvilleCountyAddresses),
                        appConfig.activeView.whenLayerView(andersonCountyAddresses),
                        appConfig.activeView.whenLayerView(pickensCountyAddresses),
                        appConfig.activeView.whenLayerView(laurensCountyAddresses),
                    ]).then(([layerView1, layerView2, layerView3, layerView4]) => {
                        addVehicleIncident_FeatureLayerView_GreenvilleCountyAddresses = layerView1;
                        addVehicleIncident_FeatureLayerView_AndersonCountyAddresses = layerView2;
                        addVehicleIncident_FeatureLayerView_PickensCountyAddresses = layerView3;
                        addVehicleIncident_FeatureLayerView_LaurensCountyAddresses = layerView4;
                        return Promise.all(
                            [
                                reactiveUtils.whenOnce(() => layerView1.updating === false),
                                reactiveUtils.whenOnce(() => layerView2.updating === false),
                                reactiveUtils.whenOnce(() => layerView3.updating === false),
                                reactiveUtils.whenOnce(() => layerView4.updating === false),
                            ]
                        );
                    }).then(() => {
                        findClosestAddress_addVehicleIncident(addressSelectorBuffer, newAddVehicleIncidentFeature, greenvilleCountyAddresses, andersonCountyAddresses, pickensCountyAddresses, laurensCountyAddresses)
                    });

Thanks for the help. 

GIS Supervisor for Greenville Water
0 Kudos