admin管理员组文章数量:1289506
The bluebird library seems to automagically use Promise::then
both as an equivalent of "map" and "flatMap" on the promise, eg see this example.
var Promise;
Promise = require('bluebird').Promise;
Promise.resolve(1).then(function(x) {
return Promise.resolve(x + 1);
}).then(function(x) {
return console.log(x); // => `2` (not a promise)
});
Promise.resolve(1).then(function(x) {
return x + 1;
}).then(function(x) {
return console.log(x); // => `2`
});
Promise.reject('hi').catch(function(x) {
return Promise.reject('hi2');
}).catch(function(x) {
return console.error(x); // => `hi2` (not a promise)
});
Is this a contract of the es6 Promise API? I see no mention of this flattening behavior here or here, for example.
The bluebird library seems to automagically use Promise::then
both as an equivalent of "map" and "flatMap" on the promise, eg see this example.
var Promise;
Promise = require('bluebird').Promise;
Promise.resolve(1).then(function(x) {
return Promise.resolve(x + 1);
}).then(function(x) {
return console.log(x); // => `2` (not a promise)
});
Promise.resolve(1).then(function(x) {
return x + 1;
}).then(function(x) {
return console.log(x); // => `2`
});
Promise.reject('hi').catch(function(x) {
return Promise.reject('hi2');
}).catch(function(x) {
return console.error(x); // => `hi2` (not a promise)
});
Is this a contract of the es6 Promise API? I see no mention of this flattening behavior here or here, for example.
Share Improve this question edited Jun 28, 2015 at 19:55 George Simms asked Jun 28, 2015 at 19:48 George SimmsGeorge Simms 4,0604 gold badges23 silver badges36 bronze badges 1-
Uh, those docs are very sparse. MSDN doesn't even mention that
then
returns a promise :-/ – Bergi Commented Jun 28, 2015 at 20:54
1 Answer
Reset to default 13Is this a contract of the es6 Promise API?
Yes, it is a contract established by Promises/A+, and has made its way from there into the ES6 specification. You will find some discussions here, here and here.
本文标签: Flattening promises in javascriptStack Overflow
版权声明:本文标题:Flattening promises in javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741397074a2376441.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论