2016-06-10 96 views
0

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); 

回答

1

这些箭头功能毫无意义。你可以做简单的

getClient().then(getClientProduct).then(prepareResponse).… 

没有方便的方法,以进一步缩短 - 我猜你不想考虑

[getClient, getClientProduct, prepareResponse].reduce((p,fn)=>p.then(fn), Promise.resolve()) 

对于最后一个,你可以使用.return(response) utility method

+0

好的,谢谢!我以为我错过了一些东西,因为这会是一个不错的功能;) –

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);