在Bluebird中是否有任何方便的解决方案链方法,其中每个元素的输入是前一个元素的解析值(除非它不是函数)?
我正在尝试将以下逻辑链接到一个方法中:
function getClient() {
// resolves with the client
}
function getClientProduct(client) {
// resolves with the product
}
var response = {}; // global response object;
function prepareResponse(product) {
// prepares global response object, resolves with `undefined`
}
promise.someMethod(getClient, getClientProduct, prepareResponse, response)
.then(data=> {
// data = the response object;
});
我想避免编写以下内容(如果可能):
getClient()
.then(client=>getClientProduct(client))
.then(product=>prepareResponse(product))
.then(()=>response);
答案 0 :(得分:1)
那些箭头功能毫无意义。你可以做到
getClient().then(getClientProduct).then(prepareResponse).…
没有简便的方法可以进一步缩短 - 我想你不想考虑
[getClient, getClientProduct, prepareResponse].reduce((p,fn)=>p.then(fn), Promise.resolve())
对于最后一个,您可以使用.return(response)
utility method。
答案 1 :(得分:1)
你真的不需要一种方便的方法。你可以这样写:
function getClient() {
// resolves with the client
}
function getClientProduct(client) {
// resolves with the product
}
var response = {}; // global response object;
function prepareResponse(product) {
// prepares global response object, resolves with `undefined`
}
getClient()
.then(getClientProduct)
.then(prepareResponse)
.return(response);