简体   繁体   中英

Return value that is within a then async function

I do not know how to return the value of the variable values ​​found in the first function and then use them as shown below with the then ().

 async escribeSolido(contenido: any) {
        const ancho = 300;
        ...
        contenido.map(async (item: any) => {

            let image = new Jimp(ancho, alto, item.color, (err: any, imagen: any) => {
                if (err) {
                    throw err;
                }
            });
            Jimp.loadFont(Jimp.FONT_SANS_32_BLACK)
            .then(font => {
                ...
                return image;
            })
            .then(async(image) => {

                let file =  uuid()+'.png';
                const path =  __dirname +'/public/'+file;
                values = await cloudinary.v2.uploader.upload(path, {folder: 'anuncios_basicos'});
                /*Return this values*/
                console.log('Values -----: ', values);           
            })          
            })
        });
        //return values;
    }

My other function

await abService.escribeSolido(contenido)
                    .then((value: any) => console.log('Values: ', value)) // undefined
                    .catch((err: any) => console.log(err))

My results

Values:  undefined
POST /api/anuncio 200 75.451 ms - 463
Values -----:  { public_id: 'anuncios_basicos/qnp6s1ptxwzxc0rrwzhp',
  ....
  url:
   'http://res.cloudinary.com/dshskwox0/image/upload/v1554922819/anuncios_basicos/qnp6s1ptxwzxc0rrwzhp.png',
  secure_url:
   'https://res.cloudinary.com/dshskwox0/image/upload/v1554922819/anuncios_basicos/qnp6s1ptxwzxc0rrwzhp.png',
  original_filename: 'e59df336-eba1-4b17-9101-fe6bfeed07dc' }

You need to return the values where your console.log statement is. This will return values to your array that is constructed from the array. To return the array of values from your escribeSolido function, simply put return infront of your map.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM