admin管理员组

文章数量:1122832

I have written a simple iterator that I'd like to break from when a specific condition is met. However, it does not do so when the condition is met. Below is the iterator and skeleton classes to show how it is implemented and called by searchByDate function. The issue is that is does not break even though it goes into the loop and the value 15 is always printed, which is the limit for the Order searches. It should break when it find the current Order, but it doesn't even though the order is found

        import { plusDays} from "./dateUtils.ts";

        import {
            isEmpty,
        } from "./collection-utils";

        export class DateIterator {
            date: Date

            limit = 15
            currentCount = 0
            constructor(date:Date) {
                this.date = date
            }


            next() {
                if (this.currentCount <= this.limit) {
                    const search = plusDays(this.date.toDateString(),1)
                    console.warn({currentNumberOfDays: this.currentCount}, 'Number of days iterated')
                    this.currentCount++
                    return { done: false, value: search}
                }
                else
                {
                    return {
                        done: true,
                        value: this.date
                    }
                }
            }
        }

        export class DateIncrementor {
            initDate: Date
            constructor(initialDate:Date) {
                this.initDate = initialDate
            }

            [Symbol.iterator]() {
                return new DateIterator(this.initDate);
            }
        }
        
        class Order {
            //...
            
            orderItems = ()
        }
        
        class OrderService {
            getOrdersForDate = async(date:string) =>{
                return new Order()
            }

            isCurrentOrder = (orderDetails:Order) =>{
                return true
            }
        }

        const searchByDate = async(date:Date) => {
            const orderService = new OrderService()
            const  nextDaySearchIterator =  new DateIncrementor(date)
            let count = 0
            let result = {} as Order
            for await (let currentDate of new DateIncrementor(date) ){
                count+=1
                if(date){
                    const orderDetails:Order = await orderService.getOrdersForDate(currentDate)
                    if(orderService.isCurrentOrder(orderDetails)) {
                        if(!isEmpty(orderDetails.orderItems)){
                            result = orderDetails
                            break
                        }
                    }
                }
            }
            console.log('Number of executions of iterator',count)
            return result? result : null
        }

本文标签: javascriptMy Iterator does not break during iterationStack Overflow