admin管理员组文章数量:1319470
Consider two examples below...
TEST 1
function test1() {
return new Promise(function () {
return 123;
});
}
test1()
.then(function (data) {
console.log("DATA:", data);
return 456;
})
.then(function (value) {
console.log("VALUE:", value);
});
It outputs nothing.
TEST 2
function test2() {
return new Promise(function (resolve, reject) {
resolve(123);
});
}
test2()
.then(function (data) {
console.log("DATA:", data);
return 456;
})
.then(function (value) {
console.log("VALUE:", value);
});
It outputs:
DATA: 123
VALUE: 456
What are the drawbacks or spec contradictions for a promise constructor not to simply resolve a returned value in TEST 1?
Why does it have to be a different result than in TEST 2?
I'm trying to understand how a constructed promise object is different from a then-able object as per the promise spec.
Consider two examples below...
TEST 1
function test1() {
return new Promise(function () {
return 123;
});
}
test1()
.then(function (data) {
console.log("DATA:", data);
return 456;
})
.then(function (value) {
console.log("VALUE:", value);
});
It outputs nothing.
TEST 2
function test2() {
return new Promise(function (resolve, reject) {
resolve(123);
});
}
test2()
.then(function (data) {
console.log("DATA:", data);
return 456;
})
.then(function (value) {
console.log("VALUE:", value);
});
It outputs:
DATA: 123
VALUE: 456
What are the drawbacks or spec contradictions for a promise constructor not to simply resolve a returned value in TEST 1?
Why does it have to be a different result than in TEST 2?
I'm trying to understand how a constructed promise object is different from a then-able object as per the promise spec.
Share Improve this question edited Sep 25, 2015 at 11:50 vitaly-t asked Sep 25, 2015 at 10:41 vitaly-tvitaly-t 26k17 gold badges127 silver badges150 bronze badges 5-
which
Promise
library is this? – Jamiec Commented Sep 25, 2015 at 10:49 - @jamiec es6 promise. – Daniel A. White Commented Sep 25, 2015 at 10:49
- @Jamiec ES6 Promise within NodeJS 4.1.1 – vitaly-t Commented Sep 25, 2015 at 10:49
- ok, I thought so but wasnt 100% – Jamiec Commented Sep 25, 2015 at 10:50
-
1
The returned value is to be ignored per the spec (as it obviously has, to be able to wait for asynchronous
resolve
calls). Check out the design of the constructor pattern. If you are looking for the differences between thenables and promises, see this post. – Bergi Commented Sep 26, 2015 at 15:03
2 Answers
Reset to default 7The function passed to Promise
isn't a callback for onFulfilled
or onRejected
. MDN calls it the executor
. Think of it as the async context that the promise is attempting to capture. Returning from an async method doesn't work (or make sense), hence you have to call resolve
or reject
. For example
var returnVal = new Promise(function() {
return setTimeout(function() {
return 27;
});
});
... does not work as intended. If you were to return a value from the executor
before your async calls finished, the promise couldn't be re-resolved.
Also, it could be ambigous with the implicit return undefined;
at the end of the function. Consider these executors that function the same way.
// A
function a() { return undefined; }
// B
function b() { }
What would tell the Promise
constructor that you really wanted to resolve with undefined
?
a() === b(); // true
It's worth mentioning that there is a shorthand for returning a resolved promise, Promise.resolve.
return new Promise(function (resolve, reject) {
resolve(123);
});
simply bees
return Promise.resolve(123);
本文标签: javascriptReturn value from a Promise constructorStack Overflow
版权声明:本文标题:javascript - Return value from a Promise constructor - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742060090a2418529.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论