【问题标题】:How to get return Value from a nested promises如何从嵌套的 Promise 中获取返回值
【发布时间】:2016-02-19 08:23:26
【问题描述】:

我在从嵌套的 Promise 和回调中返回数据时遇到一些问题,我从我的组件调用 Api 方法首先找到 checkid,当我收到 checkid 时,我将使用该 id 检查状态。这里的另一件事是,当我检查状态时,我正在使用第三方服务,因此不会立即返回数据。所以我最多调用状态 api 10 次(超时 3 秒),直到我收到我的数据。 这是我的第一个实现。

deviceService 由使用请求库的简单 API 方法组成

    For Ex: 

      const deviceService ={
        fetchID(param1){
            request.get('https://.....')
        }
      } 

//在react组件文件中。我将 API 称为

       API.getDeviceLockStatus(param1).then(data => {
           //I need data after i reciving the value from 3rd party server
          if (data) {
             //DO SOMETHING
           }
       });

API.js 文件:

            var API = {
                getDeviceLockStatus(param1) {        
                    return deviceService.fetchID(param1).then(checkID => {

                        console.log('Got checkID here')
                        if (checkID) {
                            callStatus(checkID);
                        }
                    }, handleError);
                }
            }

            function callStatus(checkID) {
                return deviceService.fetchStatus(checkID).then(status => {
                    // If i receive the response from 3rd party Server i will have status.body.isAvailable = true (or False)
                    // if Not i will recieve status with status.checkID (this checkID is what i have supplied)
                    if (status.body.isAvailable) {
                        return status
                    } else {
                        setTimeout(callBackFunction(status.checkID), 3000)
                    }
                },han)
            }
            callBackFunction(checkID) {
                callStatus(checkID);
            }
            function handleError(){
                // Error will be Handled
            }

所以问题是当我调用 API.getDeviceLockStatus(param1).. 它立即返回 undefined。

有人可以帮我做得更好,或者在获取真实数据后返回实际值吗?

提前致谢。

【问题讨论】:

  • It returns undefined immidiately. - 你没有在 .then 块中返回任何东西,所以这就是返回的 Promise 将解析为......也许你的意思是 callStatus(checkID); 在那个块......但是那么,callStatus 将不会像你期望的那样工作,如果它通过那里的 setTimeout 代码
  • 是的,我正在努力解决的一个问题,正如你所说,我在两个地方以及 setTimeout 循环之前都添加了返回 callStatus(checkId)。仍然一无所获
  • 你不能返回 setTimeout 并期望得到一个 Promise ... callStatus 函数看起来是递归的(但不是坏的方式) - 所以你需要重写代码有点

标签: rest reactjs request promise ecmascript-6


【解决方案1】:

查看 API.js,没有太多要改变的

var API = {
    getDeviceLockStatus(param1) {
        return deviceService.fetchID(param1).then(checkID => {
            console.log('Got checkID here')
            if (checkID) {
                return callStatus(checkID); // added return
            }
        }, handleError);
    }
}

function callStatus(checkID) {
    return deviceService.fetchStatus(checkID).then(status => {
        // If i receive the response from 3rd party Server i will have status.body.isAvailable = true (or False)
        // if Not i will recieve status with status.checkID (this checkID is what i have supplied)
        if (status.body.isAvailable) {
            return status
        } else { // here one way to do a simple delay/retry with a Promise
            return new Promise(function(resolve) {
                setTimeout(resolve, 3000);
            }).then(function() {
                return callStatus(status.checkID);
            });
        }
    }, handleError)
}

function handleError() {
    // Error will be Handled
}

【讨论】:

  • 老兄,你真是太棒了。我正要重写这整个该死的东西。
  • 如果这是答案,你应该接受它。
猜你喜欢
  • 2013-10-26
  • 2020-04-13
  • 2014-02-21
  • 1970-01-01
  • 2021-08-08
  • 2019-01-09
  • 1970-01-01
  • 2021-04-20
  • 1970-01-01
相关资源
最近更新 更多