2017-03-23 13 views
0

私はユーザーからのリクエストに対して多くのマイクロサービスを開発中です。どんなライブラリでも、請求、勧告 のような多くの他のサービスへのasyn呼び出しを行い、チェックと応答を戻します。REST/RPC API asynがマイクロサービスを呼び出す

例:

コールいくつかのAPI

get(request): 

    // call two services async 
    billing = billingService(user_id) //service end point 1 
    recommendation = recommendation_service(user_id) //service end point 2 

    // we will have results from those two services 

    //check conditions response to user 
    if billing == OK: 
     response (recommendation) // response 

答えて

1

あなたは上記の問題を解決するために約束を使用することができます。

billing = billingService(user_id).then(results => { 
//Here in the results variable the output of billing service method will be stored if billing service method returns a promise. 
recommendation = recommendation_service(user_id)}) 

プロミスは、javascriptで非同期呼び出しを処理する最も効率的な方法です。

あなたは、特にあなたがES7の非同期を使用しようとすることができNode.jsの

0

の「Q」のライブラリを使用することができます任意のライブラリのためsearcingされている場合は/待っています。それは書くのが簡単になり、あなたのコードももっときれいに見えます。

あなたはそれが仕事を得るたら、あなたのコードのようなものになります。

const billing = await billingService(user_id) 
const recommendation = await recommendation_service(user_id) 

を(彼らは成功したコールバック地獄から人々を保存してきたやっぱ)私は、非同期/のawait unmistifyには余りにも学習の約束をお勧めします(あなたのbillingServiceとrecommendation_serviceは最終的に非同期関数になってから待つことができます)

これは役に立ちます。

関連する問題