简体   繁体   English

为什么'新承诺(...)'返回'未定义'?

[英]Why is 'new Promise(…)' returning 'undefined'?

It seems that if the 'resolve' function is not referenced in the function that is used to create a promise, then the promise is undefined. 似乎如果在用于创建promise的函数中没有引用'resolve'函数,那么promise是未定义的。 In the code below, ... 在下面的代码中,...

var count = 0;
var limit = 3;
//
var thePromise;
function timeout(_count) {
    thePromise.resolve(_count);
}
function sendMessage() {
    return new Promise(function(resolve, reject) {
        if (++count > limit) {
            reject({'Limit Exceeded': count});
        }
        else {
// With this line in place, the return value from this function
// (expected to be a promise) is undefined
            setTimeout(timeout.bind(null, count), 1000);
// If the line above is replaced by this, it works as expected
//            setTimeout(/*timeout.bind(null, count)*/function (_count) {
//                resolve(_count);
//            }.bind(null, count), 1000);
        }
    });
}

function sendAnother(_count) {
    console.log('Resolved with count %j', _count);
    return sendMessage();
}
function detectError(_error) {
    console.log('Rejected with %s', JSON.stringify(_error));
    process.exit(1);
}
thePromise = sendMessage();
thePromise = thePromise.then(function (_count) { return sendAnother(_count)}, function(_error) {detectError(_error)});

trying to do the resolve outside of the function that creates the promise, results in: 尝试在创建promise的函数之外执行解析,导致:

node-promises.js:6
    thePromise.resolve(_count);
               ^
TypeError: undefined is not a function
    at timeout (node-promises.js:6:16)
    at Timer.listOnTimeout (timers.js:110:15)

But if line 16 is commented out and lines 18-20 are uncommented, the output is: 但是如果第16行被注释掉并且第18-20行被取消注释,则输出为:

Resolved with count 1

.. which is what I expected. ..这是我的预期。 What am I missing? 我错过了什么? This is using nodejs v0.12.2 on Windows 7, if that makes any difference. 这是在Windows 7上使用nodejs v0.12.2,如果这有任何区别。

It happens because of this line: 它发生在这条线上:

thePromise.resolve(_count);

there is no resolve function on that object. 该对象没有resolve功能。 resolve comes from the function you have created when instantiating the new promise: resolve来自实例化新promise时创建的函数:

return new Promise(function(resolve, reject) {

By commenting out that line, and using the alternate function, you are calling the correct resolve() , which causes the desired output. 通过注释掉该行,并使用alternate函数,您将调用正确的resolve() ,这将导致所需的输出。 One option to fix this could be to pass the resolve function into your timeout call, eg: 修复此问题的一个选项可能是将resolve函数传递给超时调用,例如:

function timeout(resolve, _count) {
    resolve(_count);
}

Although I am not sure why you would want to do this. 虽然我不确定你为什么要这样做。


Your title asks why new Promise is returning undefined, when the fact is that it isn't. 你的标题询问为什么new Promise返回undefined,而事实是它不是。 It is indeed returning a valid promise. 它确实返回了一个有效的承诺。 It is just that resolve is not a valid function on the promise object. 只是resolve不是promise对象上的有效函数。

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

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