[英]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.