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
版权声明:本文标题:javascript - My Iterator does not break during iteration - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736308452a1933667.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论