简体   繁体   English

JavaScript的Promise解决功能与解决匿名功能

[英]javascript promise resolve function vs resolve anonymous function

Why is this working (based on the console.log output) 为什么这样做有效(基于console.log输出)

return new Promise(function(resolve) {
    var test = function() {
        console.log('rrrr');
        return $timeout(function(){},100);
    }
    resolve(test());
}

But this is not? 但这不是吗?

return new Promise(function(resolve) {
    resolve(function() {
        console.log('rrrr');
        return $timeout(function(){},100);
    });
}

Because the top one calls the test function, but the bottom one only defines the anonymous function. 因为最上面的一个调用了test函数,但是最下面的一个仅定义了匿名函数。

Try this, it should work: 试试这个,它应该可以工作:

return new Promise(function(resolve) {
    resolve(function() {
        console.log('rrrr');
        return $timeout(function(){},100);
    }()); // the extra () will call your anonymous function.
}

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

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