简体   繁体   English

返回值位于then异步函数中

[英]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 (). 我不知道如何返回在第一个函数中找到的变量值的值,然后将它们与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. 您需要返回console.log语句所在的值。 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. 要从escribeSolido函数返回值数组,只需将return放在地图的前面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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