Node.js中Fetch不执行await等待的问题排查求助
问题:Node.js中await不生效且fetch请求未执行?
我尝试让Node.js等待fetch请求的成功或失败结果,已知fetch会返回Promise且已配置对应处理逻辑,但await未按预期生效,具体尝试的写法及问题如下:
第一种写法:带then/catch的await
实际执行时,函数直接返回调用方,既不打印@@ after fetch fetchResult=,也不执行成功/失败回调,甚至未发起指定的POST请求。
async function getAccessToken() { ... let fetchResult = await fetch(argumentParserResult.authorizationUrl, { method: 'POST', body: formData, headers: headers }).then(success => { console.log("Success reached. " + JSON.stringify(success)); process.exit(2); }, other => { console.log("Other reached. " + JSON.stringify(other)); process.exit(3); }); console.log('@@ after fetch fetchResult=' + fetchResult); ... }
第二种写法:try/catch包裹await
日志仅输出到@@getAccessToken BP7,未触发finally块、错误处理逻辑或fetch后的日志内容。
async function getAccessToken() { console.log('@@getAccessToken BP1'); if (argumentParserResult.authenticationScheme == 'OAUTH2') { console.log('@@getAccessToken BP2'); const fetch = require('node-fetch'); const url = argumentParserResult.resourceUrl; console.log('@@getAccessToken BP3'); let formData = new URLSearchParams({ 'grant_type': 'client_credentials', 'client_id': argumentParserResult.clientId, 'scope': argumentParserResult.clientScope, 'client_secret': argumentParserResult.clientSecret }) console.log('@@getAccessToken BP4'); let headers = { 'Content-Type': 'application/x-www-form-urlencoded' }; console.log('@@getAccessToken BP5'); console.log('POST ' + argumentParserResult.authorizationUrl); console.log(JSON.stringify(formData)); console.log('@@getAccessToken BP6'); try { console.log('@@getAccessToken BP7'); const response = await fetch(argumentParserResult.authorizationUrl, { method: 'POST', body: formData, headers, }); console.log('@@getAccessToken BP8'); console.log(`Success reached.`, JSON.stringify(response)); const json = await response.json(); console.log('@@getAccessToken BP9'); console.log(`Other reached.`, json); return json; } catch (error) { console.log('@@getAccessToken BP10'); console.log(`!! something went wrong`, error.message); console.error(error); return error; } finally { console.log('@@getAccessToken BP11'); console.log(`fetch finished`); } console.log('@@getAccessToken BP12'); } console.log('@@getAccessToken BP13'); return "Should not have reached this point"; }
第三种写法:带返回值的then链+await
日志仅输出到POST请求信息,之后直接返回调用方,无任何fetch请求执行的迹象。
async function getAccessToken() { console.log('@@getAccessToken BP1'); if (argumentParserResult.authenticationScheme == 'OAUTH2') { console.log('@@getAccessToken BP2'); const fetch = require('node-fetch'); const url = argumentParserResult.resourceUrl; console.log('@@BP1.9'); let formData = new URLSearchParams({ 'grant_type': 'client_credentials', 'client_id': argumentParserResult.clientId, 'scope': argumentParserResult.clientScope, 'client_secret': argumentParserResult.clientSecret }) console.log('@@getAccessToken BP3'); let headers = { 'Content-Type': 'application/x-www-form-urlencoded' }; console.log('@@getAccessToken BP4'); console.log('POST ' + argumentParserResult.authorizationUrl); console.log(JSON.stringify(formData)); let response = await fetch(argumentParserResult.authorizationUrl, { method: 'POST', body: formData, headers: headers }).then(success => { console.log('@@getAccessToken BP5'); console.log("Success reached. " + JSON.stringify(success)); return success // !--> LOOK HERE, you should return the success variable }).catch(e => { console.log('@@getAccessToken BP6'); console.log(e) // !--> LOOK HERE, if you catch the error, no error will be thrown to the caller return e }); console.log('@@getAccessToken BP7'); console.log('@@ after fetch fetchResult=', fetchResult); // !--> LOOK HERE, this log will always log something now, it could be the responso or the error } console.log('@@getAccessToken BP8'); }
请问为何await完全不生效,且fetch请求始终未执行?
内容的提问来源于stack exchange,提问作者Woodsman
相关产品推荐
相关产品推荐

