0

如何调用返回承诺的函数?

const checkDatabase = async (row: Row): Promise<boolean> => {
  return await checkDatabaseService.check()
}

使用管道,例如

parseStream(steam, {})
.validate(row: Row, processValidationError) : void => {
  processValidationError(null, checkDatabase(), 'ok')
}
.on('data-invalid', (row: Row, rowNumber: number, reason: string) => {
  console.log(`${JSON.stringify(row)} ${rowNumber} ${reason}`)
}
.on('data', row => email(row))
.on('error', error => console.error(error))
.on('end', (rowCount: number) => console.log(`Processed ${rowCount} rows.`));

.validate 调用不起作用。

4

1 回答 1

0

以下模式使用 validate 和 data-invalid 工作。

parseStream(stream, {})
  .validate((row: Winner, validationErrorCb): void => {
    validateEmail(row, validationErrorCb)
  })
  .on('data', row => this.promises.push(saveRow(row)))
  .on('data', row => this.promises.push(email(row)))
  .on('data-invalid', (row, rowNumber: number, reason: string) => {
       console.log(`Invalid [rowNumber=${rowNumber}] 
       [row=${JSON.stringify(row)}] [reason=${reason}]`)
                const errorRow = processValidationError(row, rowNumber, reason)
                this.promises.push(saveRow(errorRow))
            })

回调方法检查数据并返回验证回调 isValid: false 在失败的情况下

 const validateEmail = (row: Winner, validationErrorCb: RowValidateCallback): void => {
    const valid = row.email && !EmailValidator.validate(row.email)
    if (valid) {
      return validationErrorCb(null, true, 'ok')
    }

    row.winPayStatus = WinPayStatus.invalid
    const msg = `Key email ${row.email} is invalid.`
    console.error(msg + JSON.stringify(row))
    return validationErrorCb(null, false, msg)
  }
于 2021-08-15T21:30:44.523 回答