简体   繁体   English

JS Promise:调用一个 function 在 resolve 中有一个 return 语句

[英]JS Promise: calling a function that has a return statement inside resolve

I have a function responsible for uploading images to remote storage.我有一个 function 负责将图像上传到远程存储。

I also have another function that creates a user in a DB.我还有另一个 function 在数据库中创建用户。 During the user creation, I should upload the image, get back the URL of the uploaded image, and add it to the user object before adding it to the database.在创建用户的过程中,我要上传图片,取回上传图片的URL,添加到用户object后,再添加到数据库中。 However, I am stuck at getting back the urls:但是,我被困在取回网址:

My function that uploads images:我上传图片的 function:

export const writePicture = (picture) => {
    if (picture !== undefined) {

       //code to upload image to database (...)

                .then((url) => {
                    console.log(`finished executing writePicture, url: ${url}`) //all good here, url is console logged.
                    return url;
                });
        });
    } else {
        console.log("finished executing writePicture")
        return ""
    };
}

The above function is exported to another file where I use promises.上面的 function 被导出到另一个我使用 promises 的文件。 My promises are currently set as follows:我的承诺目前设置如下:

//pictureLogo and pictureImage are existing image objects

const waitForLogoUrl = new Promise((resolve, reject) => {
  resolve(writePicture(pictureLogo));
});

const waitForImageUrl = new Promise((resolve, reject) => {
  resolve(writePicture(pictureImage));
});

Promise.all([waitForLogoUrl, waitForImageUrl]).then((urls) => {
  //urls should be an array containing the urls from first and second promise
  console.log("the urls are:");
  console.log(urls);
});

The above is not working as expected.以上没有按预期工作。 Inside urls , I am getting an array of two undefined entries instead of the two obtained urls.urls中,我得到一个包含两个未定义条目的数组,而不是两个获得的 url。

writePicture() is working fine, it is uploading the pictures and console logging the urls. writePicture()工作正常,它正在上传图片和控制台记录 url。 However the urls are being console logged after the Promise.all has finished:然而,在Promise.all完成后,这些 URL 将被控制台记录:

https://imgur.com/lvP7rrg https://imgur.com/lvP7rrg

I am new to using promises so I am probably missing something about how to call a function inside a resolve.我不熟悉使用 Promise,所以我可能遗漏了一些关于如何在解析中调用 function 的内容。 Any help is appreciated.任何帮助表示赞赏。

Usually when new Promise appears in code, it's a red flag, and this is no exception.通常当new Promise出现在代码中时,这是一个危险信号,这也不例外。 Below I assume you cannot use async/await, and it only corrects 2 things:下面我假设你不能使用 async/await,它只纠正了两件事:

  1. Return promises from writePicture从 writePicture 返回承诺
  2. Don't unnecessarily wrap its result in another promise.不要不必要地将其结果包装在另一个 promise 中。
export const writePicture = (picture) => {
    if (picture !== undefined) {
      return codeToUploadImage(...)   // Return is critical here
         .then((url) => {
            console.log(`finished executing writePicture, url: ${url}`) //all good here, url is console logged.
            return url;
         });
    } else {
        console.log("finished executing writePicture")
        return ""
    };
}


//pictureLogo and pictureImage are existing image objects

const waitForLogoUrl = writePicture(pictureLogo);
const waitForImageUrl = writePicture(pictureImage);

Promise.all([waitForLogoUrl, waitForImageUrl]).then((urls) => {
  //urls should be an array containing the urls from first and second promise
  console.log("the urls are:");
  console.log(urls);
})

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

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