简体   繁体   English

解析Promise'when'返回未定义(JavaScript)

[英]Parse Promise 'when' returns undefined (Javascript)

I'm trying to follow the 'when' example on Parse JavaScript SDK: Parse.Promise with the following code: 我正在尝试使用以下代码遵循Parse JavaScript SDK上的“何时”示例:Parse.Promise

GetFromDB2 = function(id) {
    var DB2 = Parse.Object.extend("DB2");
    var q = new Parse.Query(DB2);
    q.get(id, {
        success: function(res) {
            return Parse.Promise.as(10);
        },
        error: function(res, err) {
            console.log( err);
        }
    });
}

GetData = function() {
    var DB1 = Parse.Object.extend("DB1");
    var query = new Parse.Query(DB1);
    query.equalTo("param", "close");

    query.find().then(function(results) {
        var promises = [];
        _.each(results, function(res) {
            promises.push(GetFromDB2(res.get("user")));
        });

        Parse.Promise.when(promises).then(function() {
            console.log(arguments); // expect: [10, 10, ...]
        })
    }); 
};

The length of the array arguments is correct but not sure why its values are undefined. 数组参数的长度正确,但是不确定为什么其值未定义。

As written, GetFromDB2() returns undefined . 如所写, GetFromDB2()返回undefined To deliver a value, it must return either a value or a promise. 要交付价值,它必须返回价值或承诺。

At its simplest, to deliver 10 , you could write : 最简单地说,要交付10 ,您可以编写:

function GetFromDB2(id) {
    return 10;
}

But to be asynchronous, and still deliver 10 , you need to return a promise that will resolve to 10 : 但是要保持异步,并仍然交付10 ,则需要返回一个承诺,该承诺将解决为10

function GetFromDB2(id) {
    return Parse.Promise.as(10);
}

Or the .get(id) query you really want : 或您真正想要的.get(id)查询:

function GetFromDB2(id) {
    var DB2 = Parse.Object.extend('DB2');
    var q = new Parse.Query(DB2);
    return q.get(id).then(function(res) {
        return 10;
    });
    //omit `.then(...)` entirely to deliver `res`
}

Now GetData() can be written as follows : 现在, GetData()可以编写如下:

function GetData() {
    var DB1 = Parse.Object.extend('DB1');
    var query = new Parse.Query(DB1);
    query.equalTo('param', 'close');
    return query.find().then(function(results) {
        var promises = _.map(results, function(res) {
            return GetFromDB2(res.get('user'));
        });
        Parse.Promise.when(promises).then(function() {
            console.log(arguments); // expect: [10, 10, ...]
        }, function(err) {
            console.log(err);
            return err;
        });
    }); 
};

Notes: 笔记:

  • promises = _.map(...) is more elegant than _.each(...) plus `promises.push(...). promises = _.map(...)_.each(...)加上`promises.push(...)更优雅。
  • moving the error handler into GetData() allows a greater range of possible errors to be handled, eg an error arising from query.find() . 将错误处理程序移至GetData()可以处理更大范围的可能错误,例如query.find()引起的错误。
  • By returning a promise, GetData()'s caller is also informed of the eventual asynchronous outcome. 通过返回一个promise,GetData()的调用方也将被告知最终的异步结果。

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

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