繁体   English   中英

Promise 比预期更早解析并且未返回 AXIOS 调用值

[英]Promise resolves earlier than expected and not returning an AXIOS call value

我正在向一个快速连续生成多个字符串的服务发出请求。 我的问题是因为必须使用此服务返回 promise,因为我不控制返回字符串的服务进程。

我想强调一个事实,我必须返回一个 promise。

现在,我拥有的是主要的 function (handler.ts),它不维护任何业务逻辑,仅包括以下内容:

public static callback: any;

public static async init(configuration, callback): Promise<any> {
     this.callback = callback;
     ...
     return new Promise((resolve, reject) => {
        try {
            const result = await Service.bootstrap(configuration)
            return resolve(result)
        } catch(err) {
            reject(err)
        }
     }
}

此处理程序调用服务,该服务具有引导程序 function,该服务执行对 a.js 文件的调用,该文件从我的计算机获取一些信息,然后返回有关它的字符串。

import loadComputerInformation from 'load.js'

public async bootstrap() : Promise<any> {
      loadComputerInformation();

      function useComputerInfoString() {
            // window.getInfo is generated by loadComputerInformation(), and I can not control 
            // when does it return it, as it generates several strings with rapid succession 
            // and until the function exists, I will not get my string.
            if (typeof window.getInfo !== 'function') {return;}
            const data = window.getInfo();
            if (data.finished) {
              clearTimeout(timeoutId);
              const infoString = data.computerInfo;
              Service.axiosCall(infoString);
            }
          }
          // I have to set an interval to perform the call several times, and until it resolves it
          // it will not stop performing this call.
          const timeoutId = setInterval(useComputerInfoString, 500);
   }
   return;
}

因此,我面临的问题是我的 promise 在另一个线程中丢失了,我无法从Service.axiosCall(infoString)返回值,这只是一个标准的 axios 调用,但这必然需要infoString

添加 axios 调用 function 以防它有用。 现在,我正在使用传递给 handler.js 的回调返回 axios 调用,但我希望能够将其包含在 Promise 中,而无需回调 function

public static async axiosCall(blackbox): Promise<any> {
   await axios.post('https://somecall.com/info', blackbox)
    .then((response) => {  this.callback(element)
                           return element);
     }
}

知道如何解决这个问题吗?

强调

请注意,loadComputerInformation() 异步加载 Window.getInfo(),但它不只解析一个值,而是解析多个值,因此我不能在 Window.getInfo() 上等待,因为在开始时它不存在,只会返回不明确的

此外,现在代码已启动并运行,但它返回值的方式是通过回调而不是 promise。

尝试bootstrap function,其返回的 promise 解析为 axios 调用返回的响应

此建议(基于帖子中的信息)是 vanilla JavaScript,用于演示如何解决该问题。 显然需要修改以将其集成到应用程序中:

 const bootstrap = () => new Promise( resolve => {
    loadComputerInformation();
    let state = "load";
    const timer = setInterval( ()=> {
       if( state == "load") {
           if( typeof window.getData != "function") {
                return;
           }
           state = "get";
       }
       let data;
       if( state == "get") {
           data = window.getData();
           if(!data.finished) {
               return;
           }
           // state = "request";
       }
       clearInterval(timer);
       resolve( axios('post', "https:example.com/info", data.computerInfo) );

    }, 100); // 1/10th second?
};

请注意,此答案已修改为使用 state 机器等待异步加载getData ,然后等待调用getData以返回具有finished属性的数据 object,然后再解析返回的 promise 和 axios 返回的axios调用 - 第一个答案很简单,直到它需要等待异步加载getData代码。

问题的背后是一个可能不得不注销代码债务的问题:JavaScript 是事件驱动的。 loadComputerInformation似乎是为要轮询的结果而编写的。 很难想象它留在那个 state 中的正当理由。

暂无
暂无

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

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