Skip to main content
make title describe what code does instead of goals - refer to help center section "Titling your question" on https://codereview.stackexchange.com/help/how-to-ask
Source Link

Avoid functions chain Get images for PDFs from URLs

I Have the below code that gets urls of PDF files, send them to a convert function and returns them as images, is there a way to make this code more readable?

Is there a way to avoid functions chaining?

import PDFtoImages from '../PDFtoImages'
    
export async function convertPDF(file, category) {
const images= await PDFtoImages(file.data)
return {
   category: category ? [category] : [],
    images
  }
}
    
export function fetchBlob(url, opts = {}) {
  return fetch(url, opts).then(res => {
    if (!res.ok) return Promise.reject(new Error(res.statusText))
      return res.blob()
    })
}

export async function fetchFilesAsImages(filesUrls) { 
  const pdfAsImages= []
  await Promise.all(
    filesUrls.map(async file=> {
      pdfAsImages.push(
        await convertPDF(
          {data: await fetchBlob(file.url)},
          file.category
        )
      )
    })
  )
  return pdfAsImages
}

Avoid functions chain

I Have the below code that gets urls of PDF files, send them to a convert function and returns them as images, is there a way to make this code more readable?

import PDFtoImages from '../PDFtoImages'
    
export async function convertPDF(file, category) {
const images= await PDFtoImages(file.data)
return {
   category: category ? [category] : [],
    images
  }
}
    
export function fetchBlob(url, opts = {}) {
  return fetch(url, opts).then(res => {
    if (!res.ok) return Promise.reject(new Error(res.statusText))
      return res.blob()
    })
}

export async function fetchFilesAsImages(filesUrls) { 
  const pdfAsImages= []
  await Promise.all(
    filesUrls.map(async file=> {
      pdfAsImages.push(
        await convertPDF(
          {data: await fetchBlob(file.url)},
          file.category
        )
      )
    })
  )
  return pdfAsImages
}

Get images for PDFs from URLs

I Have the below code that gets urls of PDF files, send them to a convert function and returns them as images, is there a way to make this code more readable?

Is there a way to avoid functions chaining?

import PDFtoImages from '../PDFtoImages'
    
export async function convertPDF(file, category) {
const images= await PDFtoImages(file.data)
return {
   category: category ? [category] : [],
    images
  }
}
    
export function fetchBlob(url, opts = {}) {
  return fetch(url, opts).then(res => {
    if (!res.ok) return Promise.reject(new Error(res.statusText))
      return res.blob()
    })
}

export async function fetchFilesAsImages(filesUrls) { 
  const pdfAsImages= []
  await Promise.all(
    filesUrls.map(async file=> {
      pdfAsImages.push(
        await convertPDF(
          {data: await fetchBlob(file.url)},
          file.category
        )
      )
    })
  )
  return pdfAsImages
}
Source Link

Avoid functions chain

I Have the below code that gets urls of PDF files, send them to a convert function and returns them as images, is there a way to make this code more readable?

import PDFtoImages from '../PDFtoImages'
    
export async function convertPDF(file, category) {
const images= await PDFtoImages(file.data)
return {
   category: category ? [category] : [],
    images
  }
}
    
export function fetchBlob(url, opts = {}) {
  return fetch(url, opts).then(res => {
    if (!res.ok) return Promise.reject(new Error(res.statusText))
      return res.blob()
    })
}

export async function fetchFilesAsImages(filesUrls) { 
  const pdfAsImages= []
  await Promise.all(
    filesUrls.map(async file=> {
      pdfAsImages.push(
        await convertPDF(
          {data: await fetchBlob(file.url)},
          file.category
        )
      )
    })
  )
  return pdfAsImages
}