2016-06-10 94 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);