Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

107
Vistas
Refactor non-standard library callback into promise

I am using the fabricjs package and want to make use of the fabric.Image.fromURL function. The documentation for this function is below:

(static) fromURL(url, callbackopt, imgOptionsopt)

Creates an instance of fabric.Image from an URL string
Parameters:

Name Type Attributes Description
url String URL to create an image from
callback function <optional> Callback to invoke when image is created (newly created image is passed as a first argument). Second argument is a boolean indicating if an error occurred or not.
imgOptions Object <optional> Options object

I would to refactor my code to use modern Promise notation instead of nesting callbacks. Can I refactor this function into a Promise?

Currently if I want to add two images in a row, my code looks like this:

  # add first image
  fabric.Image.fromURL(url1, function (oImg: any) {
    canvas.add(oImg);
    # add second image
    fabric.Image.fromURL(url2, function (oImg: any) {
      canvas.add(oImg);
    }  
  })

I would love to be able to rewrite it as

async def addImages() {
    await promisifiedImageFromURL(url1)
    await promisifiedImageFromURL(url2)
}

I have looked into util.promisify (https://nodejs.org/api/util.html#utilpromisifyoriginal) but this function expects the corresponding function to be in a specific format: a single error-first callback. The fromURL function, on the other hand, is in a nonstandard format, which is where I am having trouble

I'm looking into custom promisified functions, but I'm honestly in over my head here

about 4 years ago · Santiago Gelvez
1 Respuestas
Responde la pregunta

0

const promiseFromUrl = (url, imgOptions) => {

  return new Promise((res, rej) => {

    const callback = (image, isError) => {
      if (isError) rej(new Error('Some error occurred'));
      else res(image);
    };
    fromURL(url, callback, imageOptions);

  });

}
about 4 years ago · Santiago Gelvez Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda