javascript 为什么“new Promise(...)”返回“undefined”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32065202/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 14:47:57  来源:igfitidea点击:

Why is 'new Promise(...)' returning 'undefined'?

javascriptnode.jspromise

提问by Peter Faller

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. In the code below, ...

似乎如果在用于创建承诺的函数中没有引用 'resolve' 函数,那么该承诺是未定义的。在下面的代码中,...

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:

尝试在创建承诺的函数之外进行解析,结果:

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,如果这有什么不同的话。

回答by Matt Way

It happens because of this line:

这是因为这一行:

thePromise.resolve(_count);

there is no resolvefunction on that object. resolvecomes from the function you have created when instantiating the new promise:

resolve该对象上没有功能。resolve来自您在实例化新承诺时创建的函数:

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. One option to fix this could be to pass the resolve function into your timeout call, eg:

通过注释掉该行并使用备用函数,您正在调用正确的resolve(),这会导致所需的输出。解决此问题的一种选择可能是将 resolve 函数传递到您的超时调用中,例如:

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

Although I am not sure why you would want to do this.

虽然我不确定你为什么要这样做。



Your title asks why new Promiseis returning undefined, when the fact is that it isn't. It is indeed returning a valid promise. It is just that resolveis not a valid function on the promise object.

您的标题询问为什么new Promise返回 undefined,而事实并非如此。它确实返回了一个有效的承诺。只是这resolve不是 promise 对象上的有效函数。