简体   繁体   English

为什么我下面的异步 function 得到错误代码 'Uncaught SyntaxError: Identifier 'message' has already been declared”?

[英]Why does my asynchronous function below get the error code 'Uncaught SyntaxError: Identifier 'message' has already been declared"?

My asynch function is as follows我的异步function如下

----------------------------------------- --------------------------------------
 /* async/await */ async function demoPromise() { try { let message = await myPromise; let message = await helloPromise(); console.log(message); } catch ((error) => { console.log("Error:" + error.message); }) } /* call the async function */ (async () => { await myDate(); })();

The myPromise code block is: myPromise 代码块是:

============================ ============================
 const myPromise = new Promise((resolve, reject) => { const condition = true; if (condition) { setTimeout(function() { resolve("Promise is resolved;"), // fulfilled }; 300); } else { reject('Promise is rejected;'); } });

And the `helloPromise() code block is:- `helloPromise() 代码块是:-

 const helloPromise = function() { return new Promise(function(resolve, reject) { const message = `Hi, How are you;`; resolve(message) }); }

First of all you cannot declare 2 variables with the same name so either remove let from the 2nd message variable (this will override the promise result though) or give the 2nd message variable another name ex.首先,您不能声明 2 个具有相同名称的变量,因此要么从第二个消息变量中删除let (尽管这将覆盖 promise 结果),或者为第二个消息变量指定另一个名称 ex。 let anotherMessage = await helloPromise();

Moreover, i think the 1st promise myPromise is missing a () .此外,我认为第一个 promise myPromise缺少一个() It should be let message = await myPromise();应该是let message = await myPromise();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我的代码在控制台中显示“Uncaught SyntaxError: Identifier 'calculator' has already been declared'? - why is my code saying' Uncaught SyntaxError: Identifier 'calculator' has already been declared' in the console? 为什么会出现 Uncaught SyntaxError: Identifier has already been declared 错误? - Why does Uncaught SyntaxError: Identifier has already been declared error occurs? Uncaught SyntaxError: Identifier '[x]' has been declared - 为什么? - Uncaught SyntaxError: Identifier '[x]' has already been declared - why? 未捕获的语法错误:标识符已被声明 - Uncaught SyntaxError: Identifier has already been declared 未捕获的 SyntaxError:标识符 '$...' 已被声明 - Uncaught SyntaxError: Identifier '$…' has already been declared 为什么我会收到“未捕获的语法错误:标识符 'randFilmIndex' 已被声明”错误? - Why am I getting a 'Uncaught SyntaxError: Identifier 'randFilmIndex' has already been declared' error? var vs const; 为什么 const 会产生错误:Uncaught SyntaxError: Identifier 'userOne' has been declared? - var vs const; why const is producing error: Uncaught SyntaxError: Identifier 'userOne' has already been declared? javascript-未捕获的语法错误:标识符 * 已被声明 - javascript- Uncaught SyntaxError: Identifier * has already been declared 未捕获的SyntaxError:标识符'baseUrl'已被声明 - Uncaught SyntaxError: Identifier 'baseUrl' has already been declared 未捕获的 SyntaxError:标识符“按钮”已被声明 - Uncaught SyntaxError: Identifier 'Buttons' has already been declared
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM