JavaScript Promise Method不返回任何数据

时间:2018-02-25 20:58:41

标签: javascript react-native

我正在创建一个反应原生应用程序。

我有一个触发函数findItem的后退按钮。 findItem使用异步方法searchJson。 searchJson搜索递归json以基于id查找父对象。但是它永远不会返回任何结果。

则findItem:

 findItem() {
    //Pass null so top level json will be pulled
    let result = this.searchJson(null).done();

    let abv = 2;

    // this.setState(previousState => {
    //     return {
    //         data: result,
    //         parentID: result.parentid
    //     };
    // });

}

searchJson:

   async searchJson(object) {
    return new Promise(resolve => {
        //use object or pull from porp - all data
        let theObject = object == null ? this.props.data : object;

        var result = null;
        if (theObject instanceof Array) {
            for (var i = 0; i < theObject.length; i++) {
                result = this.searchJson(theObject[i]);
                if (result) {
                    break;
                }
            }
        }
        else {
            for (var prop in theObject) {
                console.log(prop + ': ' + theObject[prop]);
                if (prop == 'id') {
                    if (theObject[prop] == this.state.parentID) {
                        return theObject;
                    }
                }
                if (theObject[prop] instanceof Object || theObject[prop] instanceof Array) {
                    result = this.searchJson(theObject[prop]);
                    if (result) {
                        break;
                    }
                }
            }
        }

        if(result != null)
            resolve(result);

    });
}

任何帮助将不胜感激。

0 个答案:

没有答案