2017-06-17 152 views
0

我有一個名爲findProperty承諾的功能,在這種情況下,拒絕這樣的:未處理的承諾,拒絕嘗試

reject('ERR: Unknown propertyID or inactive property in call of newBooking'); 

這是我的處理程序調用findProperty

async function main() { 
    var res = ""; 
    try { 

     findProperty(); 
     res = await findUser(userEmail); 
     res = await findUser(agentEmail); 
     res = await getUsers(); 

    } 
    catch(err) { 
     console.error(err); 
     console.log(" newBooking: " + err); 
     callback({ error:true, err }); 
    } 
} 
main(); 

這導致以下錯誤:

(node:10276) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): ERR: Unknown propertyID or inactive property in call of newBooking 
(node:10276) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code. 

我沒有得到這個,我去我的catch(err)應該不夠嗎?

剛試過的東西,這工作得很好:

resolve('ERR: Unknown propertyID or inactive property in call of newBooking'); 

但這給出了錯誤:

reject('ERR: Unknown propertyID or inactive property in call of newBooking'); 

回答

1

如果findProperty返回一個承諾,你需要await它才能觸發內的故障異步函數的上下文;否則排斥會消失在外層空間。

要「射後不理」無需等待,又趕上失敗與你try/catch

findProperty().catch(e => { throw e; }); 
+0

所以你不能正常使用的承諾,你不希望等待一個答覆?我以爲你可以混合搭配。 – torbenrudgaard

+0

其實你是對的@torazaburo - 一旦我把'res = await findProperty();'它工作得很好。那麼我的問題是,如何運行我所有的火,忘記承諾?我有很多地方我不想等待結果。 – torbenrudgaard

+0

@torbenrudgaard是的。但是,你需要提供一個像findProperty()catch這樣的catch(console.log.bind(console));或者根本不拒絕他們 –

相關問題