[英]Node: Unhandled promise rejection
我正在通過一個名為 promise-it-wont-hurt 的異步編程 js/node 研討會。 我有以下練習:
parsePromised
的 function 創建一個 promise,執行JSON.parse
在try
/ catch
塊中,並根據是否拋出錯誤來滿足或拒絕 promise。 注意:你的 function 應該同步返回 promise!我的答案:
function parsePromised(json) {
return new Promise(function(resolve, reject){
resolve( JSON.parse(json) ),
reject(throwError)
});
}
parsePromised(process.argv[2]).then(console.log);
堆棧跟蹤是:
(node:3499) UnhandledPromiseRejectionWarning: SyntaxError: Unexpected token u in JSON at position 0
at JSON.parse (<anonymous>)
at /home/optionhomes11/nodeprojects/promise-shop/test.js:184:21
at new Promise (<anonymous>)
at parsePromised (/home/optionhomes11/nodeprojects/promise-shop/test.js:183:10)
並且練習結果給出:
Your solution to Throw an error didn't pass. Try again!
知道如何讓它工作嗎?
更改為答案代碼后,堆棧跟蹤:
(node:5533) UnhandledPromiseRejectionWarning: SyntaxError:
Unexpected token u in JSON at position 0
at JSON.parse (<anonymous>)
at /home/optionhomes11/nodeprojects/promise-shop/test.js:186:34
at new Promise (<anonymous>)
at parsePromised (/home/optionhomes11/nodeprojects/promise-shop/test.js:184:10)
at Object.<anonymous> (/home/optionhomes11/nodeprojects/promise-shop/test.js:196:1)
at Module._compile (internal/modules/cjs/loader.js:999:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
at Module.load (internal/modules/cjs/loader.js:863:32)
at Function.Module._load (internal/modules/cjs/loader.js:708:14)
at Function.executeUserEntryPoint [as runMain]
(internal/modules/run_main.js:60:12)
(node:5533) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
根據您提到的說明,我認為您想這樣做
function parsePromised(json) {
return new Promise(function(resolve, reject){
try {
const parsedJson = JSON.parse(json);
resolve(parsedJson);
} catch(error) {
// we reach in this block if we encounter error while parsing some invalid given json
reject(error);
//or reject(error.message);
}
});
}
現在你這樣稱呼它
parsePromised(process.argv[2])
.then(console.log)
.catch(function(err) {
console.log("some error occured while parsing the json");
console.log("error is:", err.message);
});
基本上,當您嘗試獲得 promise 結果時,您在 promise 中拒絕的任何內容都會捕獲更高階的 function。
你快到了,你只是錯過了最后一步:
構建一系列步驟,如上所示,捕獲任何拋出的錯誤並將它們記錄到控制台。
如果結果不是錯誤,您只會記錄結果。 將其更改為
parsePromised(process.argv[2]).catch(console.log);
// ^^^^^
另請注意,您不需要顯式調用reject
- Promise
構造函數已經處理了從 executor 拋出的異常。 所以你需要做的就是
function parsePromised(json) {
return new Promise(function(resolve, reject){
resolve( JSON.parse(json) )
});
}
如果你想自己捕捉錯誤,你寧願寫
function parsePromised(json) {
try {
return Promise.resolve( JSON.parse(json) );
} catch(thrownError) {
return Promise.reject(thrownError);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.