Accessing Elements from Data Source Collections using getByName()

I’m trying to explore ways to organize my entities so that I can easily remove them from my map when needed. I’m looking into Custom Data Sources and Data Source Collections.

        const dataSource = new Cesium.CustomDataSource("1");

        const rectangle = dataSource.entities.add({
            name: 'rectangle',
            rectangle: {
                coordinates: Cesium.Rectangle.fromDegrees(-92.0, 20.0, -86.0, 27.0),
                outline: true,
                outlineColor: Cesium.Color.WHITE,
                outlineWidth: 4,
                stRotation: Cesium.Math.toRadians(45),
                material: stripeMaterial,
            }
        });

        const point = dataSource.entities.add({
            name: 'waypoint',
            position: Cesium.Cartesian3.fromDegrees(-95.18335, 44.95854, 0),
            point: {
                color: Cesium.RED,
                pixelSize: 10
            }
        });

        viewer.dataSources.add(dataSource);
        console.log(viewer.dataSources.contains(dataSource));
        console.log(viewer.dataSources.getByName("1"));

Why isconsole.log(viewer.dataSources.contains(dataSource)); producing false? When I console.log the viewer and look at the dataSources, I see that the dataSource is there.

Additionally, is there any better way to go about organizing my entities?

I realized that .add() returns a promise.

        viewer.dataSources.add(dataSource)
            .then((result) => {
                console.log(viewer.dataSources.contains(dataSource));
                console.log(viewer.dataSources.getByName("1"));
            })
            .catch((e) => {
                console.log(e);
            })