【问题标题】:Return value that is within a then async function在 then 异步函数中的返回值
【发布时间】:2019-04-11 03:24:53
【问题描述】:

不知道怎么把第一个函数中找到的变量values的值返回,然后用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;
    }

我的其他功能

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

我的结果

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' }

【问题讨论】:

    标签: javascript node.js async-await es6-promise


    【解决方案1】:

    您需要返回您的 console.log 语句所在的值。这会将值返回到从数组构造的数组。要从您的 escribeSolido 函数返回值数组,只需将 return 放在您的地图前面。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-27
      • 1970-01-01
      • 2022-01-14
      • 2021-11-30
      • 2015-08-24
      相关资源
      最近更新 更多