import axios from 'axios'
export const findNumberOfPages = async pdfUrl => {
return new Promise(async (resolve, reject) => {
try {
const response = await axios.get(pdfUrl, {
headers: { 'Content-Type': 'application/pdf' }
})
const reader = new FileReader()
reader.readAsBinaryString(new Blob([response.data]))
reader.onloadend = () => {
const pages = reader.result.match(/\/Type[\s]*\/Page[^s]/g)
const count = pages?.length
resolve(count)
}
} catch (error) {
reject(new Error(`Error when fetching pdf: ${error.message}`))
}
})
}
I'm adding a utility to get number of pages in a pdf(https url) using File Reader API.
But eslint shows an error, Promise executor functions should not be async. (no-async-promise-executor)standard(no-async-promise-executor)
, and I want to await the axios request to get the pdf response. How can i await the axios request and resolve the number of pages on onloadend
of FileReader?