使用 Bluebird 链接结果
Chaining results with Bluebird
在 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);
那些箭头函数毫无意义。你可以简单地做
getClient().then(getClientProduct).then(prepareResponse).…
没有进一步缩短的便捷方法 - 我猜你不想考虑
[getClient, getClientProduct, prepareResponse].reduce((p,fn)=>p.then(fn), Promise.resolve())
对于最后一个,您可以使用.return(response)
utility method。
您真的不需要便捷方法。你可以这样写:
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);
在 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);
那些箭头函数毫无意义。你可以简单地做
getClient().then(getClientProduct).then(prepareResponse).…
没有进一步缩短的便捷方法 - 我猜你不想考虑
[getClient, getClientProduct, prepareResponse].reduce((p,fn)=>p.then(fn), Promise.resolve())
对于最后一个,您可以使用.return(response)
utility method。
您真的不需要便捷方法。你可以这样写:
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);