本文介绍了Promise 的 resolve/reject 函数的返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
考虑这种情况.
new Promise(function(resolve, reject) {
var x = resolve(2);
});
x
将是什么值?我试图打印它,它显示我undefined
.它很直观,但总是如此吗?它在文档中吗?
What value will x
be?I tried to print it and it showed me undefined
. It is intuitive, but is it always so? Is it in docs?
第二个问题
new Promise(function(resolve, reject) {
resolve(2);
return 5;
});
我们应该从我们放入 Promise 的函数中返回什么?这个值被忽略了吗?
What should we return from the function that we put into a promise? Is this value ignored?
推荐答案
promise 构造函数的返回值 被忽略.
The return value of the promise constructor is ignored.
resolve
函数也返回未定义.
这首先在 promise 构造函数规范中指定,后来在 ES2015 (ES6) 语言规范.
This was first specified in the promise constructor spec and later in the ES2015 (ES6) language specification.
这篇关于Promise 的 resolve/reject 函数的返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!