2017-05-13 7 views
0

私はbluebirdを私のPromiseとして使っています。私は次のように動作しませんを意味し、その中に約束を返してくれません。

function a() { 
    return Promise.all([promise1, promise2]).then((results) => {  
     return promise3(results) 
    }) 
} 

しかし、私はpromise3の結果を得ることができませんでした:私は、次のチェーンを持っている

a().then((result) => { 
    console.log(result) 
}).catch((err) => { 
    console.error(err) 
}) 

I「の検索や約束について読ん私はcouldn問題がどこにあるのか理解できませんか?

+1

正確に 'promise3'とは何ですか? – Boaz

+0

@Boazこれは、DBクエリで約束を返す関数です。 '.then'を追加すると、クエリ結果が表示されます(' promise3'はうまく動作します) – Hadi

答えて

5

これはいくつかの理由による可能性があり、投稿からどれを判断するのか不可能です。だから、未来の読者を支援する - のは、上記のシナリオで間違って行くことができるかを把握しましょう:

はのは、コードに次のように調整してみましょう:

function a() { 
    console.log("a called"); // if this doesn't log, you're redefining 'a' 
    return Promise.all([promise1, promise2]).then((results) => { 
     console.log("In .all"); // if this isn't called, promise1 or promise2 never resolve  
     return promise3(results); 
    }).tap(() => 
    console.log("Promise 3 fn resolved") // if this doesn't log, promise3 never resolves 
    }); 
} 

その後、私たちのthenハンドラ:

let r = a().then((result) => { 
    console.log('result', result); // this logs together with promise 3 fn 
}); // bluebird automatically adds a `.catch` handler and emits unhandledRejection 

setTimeout(() => { 
    // see what `r` is after 30 seconds, if it's pending the operation is stuck 
    console.log("r is", r.inspect()); 
}, 30000); 

これは間違っている可能性のあるすべてのケースについて完全にカバーするはずです。

Promiseコンストラクタが決してresolveまたはrejectと呼ばれていないため、約束の1つが解決されません。

(ブルーバードは、特定のシナリオでは、この反対警告が表示されます - あなたは上の警告を持っていることを確認してください)これを引き起こす可能性があり

別のシナリオはキャンセルです - 私はあなたがそれがオンになっていないと仮定しています。

ハッピーコーディング。

関連する問題