如何与 2 个新 Promise 相互通信

发布于 2025-01-09 12:51:19 字数 1375 浏览 0 评论 0原文

所以我试图将 2 个新的 Promise 相互连接起来,这样我就可以互相发送信息。所以我想使用在第一个承诺中创建的 image.url 在第二个承诺中使用,但是当我想调用 image.url 时它是未定义的。有人有想法让这项工作成功吗? (顺便说一句,代码之间没有什么区别,只是想在 2 个 Promise 之间做出区分)。

第一个承诺:

        image.promise = new Promise((resolve, reject) => {
          const formData = new FormData();
          formData.append('files', file);
          axios
            .post('http://api.resmush.it/', formData, { headers: { 'Content-Type': 'multipart/form-data' } })
            .then(response => {
              image.url = response.data.dest;
              console.log("The compressed image url: ", image.url)
            })
            .catch(() => reject());
        })
        .then(response => {
          image.status = STATUS_OK;
          image.savings = response.data.percent;
        })

第二个承诺:

        image.promise = new Promise((reject) => {
        console.log(image.url);
        function downloadFile() {
          axios
            .get('http://par3.static.resmush.it/1e5b76fc993169757c1319c027f5dca3/insta_logo.jpg')
            .then(response => {console.log(response)})
            // .then(response => resolve(response))
            .catch(() => reject());
            // .catch(reject => {console.log(reject)})
        }

        downloadFile();
       }

So I am trying to connect 2 new Promise with each other so I can send information to each other. So I want to use image.url which is made in the first promise be used in the second promise but when I want to call image.url it is undefined. Does anyone have an idea to make this work? (Btw there is nothing between the code just want to make an difference between the 2 Promise).

First Promise:

        image.promise = new Promise((resolve, reject) => {
          const formData = new FormData();
          formData.append('files', file);
          axios
            .post('http://api.resmush.it/', formData, { headers: { 'Content-Type': 'multipart/form-data' } })
            .then(response => {
              image.url = response.data.dest;
              console.log("The compressed image url: ", image.url)
            })
            .catch(() => reject());
        })
        .then(response => {
          image.status = STATUS_OK;
          image.savings = response.data.percent;
        })

Second Promise:

        image.promise = new Promise((reject) => {
        console.log(image.url);
        function downloadFile() {
          axios
            .get('http://par3.static.resmush.it/1e5b76fc993169757c1319c027f5dca3/insta_logo.jpg')
            .then(response => {console.log(response)})
            // .then(response => resolve(response))
            .catch(() => reject());
            // .catch(reject => {console.log(reject)})
        }

        downloadFile();
       }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

微暖i 2025-01-16 12:51:19

我不确定您为什么要使用此模式,但是如果您想确保第二个 Promise 可以访问第一个调用中定义的内容,则必须确保在之前不要调用第二个 Promise第一个已经解决了。
也许是这样的:

// ...
         axios
            .post('http://api.resmush.it/', formData, { headers: { 'Content-Type': 'multipart/form-data' } })
            .then(response => {
              image.url = response.data.dest;
              // not sure what you want to do here, I just pasted your code
              console.log(image.url);
              function downloadFile() {
                  axios.get('http://par3.static.resmush.it/1e5b76fc993169757c1319c027f5dca3/insta_logo.jpg')
                     .then(response => {console.log(response)})
                     // ...
              }

              downloadFile();
            })

I'm not sure to understand why you want to use this pattern, but if you want to make sure the second promise can have access to something defined in the first call, you'd have to make sure not to call the second promise before the first one has been resolved.
Maybe something like this:

// ...
         axios
            .post('http://api.resmush.it/', formData, { headers: { 'Content-Type': 'multipart/form-data' } })
            .then(response => {
              image.url = response.data.dest;
              // not sure what you want to do here, I just pasted your code
              console.log(image.url);
              function downloadFile() {
                  axios.get('http://par3.static.resmush.it/1e5b76fc993169757c1319c027f5dca3/insta_logo.jpg')
                     .then(response => {console.log(response)})
                     // ...
              }

              downloadFile();
            })
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文