嗨,我找不到級聯函式問題的解決方案,
服務A
print(await serviceB.methodA(myParameter));
服務乙
async methodA(MyParameter){
return await methodB(MyParamter).then((value) =>{
serviceA.methodC(value);
}
);
}
所以輸出是
undefined
result
我怎么能等到第二個結果呢?因為當變得未定義時會破壞我的服務 A
uj5u.com熱心網友回復:
使用您的代碼結構,您需要回傳內部承諾。
改變這個:
async methodA(MyParameter){
return await methodB(MyParamter).then((value) =>{
serviceA.methodC(value);
});
}
對此:
async methodA(MyParameter){
return methodB(MyParamter).then((value) =>{
return serviceA.methodC(value);
});
}
的結果.then()成為承諾鏈的決議值。由于您沒有從.then()處理程式回傳任何內容,因此決議的值變為undefined.
但是,由于您使用的是async/await,因此通常最好不要混入.then()相同的代碼,因此我建議您更改為:
async methodA(MyParameter){
let value = await methodB(MyParamter);
return serviceA.methodC(value);
}
uj5u.com熱心網友回復:
您沒有回傳任何內容,這就是您的 print(methodA) 錯誤的原因,使用這些代碼它將起作用:
async methodA(MyParameter) {
return await methodB(MyParamter).then((value) =>
serviceA.methodC(value).then((result) => {
console.log("result");
return result;
})
);
console.log("hi");
};
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/405311.html
標籤:
上一篇:<style>禁用未應用
