Javascript 等待仅在异步函数中有效
我在Javascript 等待仅在异步函数中有效,javascript,node.js,Javascript,Node.js,我在lib/helper.js var myfunction = async function(x,y) { .... return [variableA, variableB] } exports.myfunction = myfunction; 然后我尝试在另一个文件中使用它 var helper = require('./helper.js'); var start = function(a,b){ .... const result = awa
lib/helper.js
var myfunction = async function(x,y) {
....
return [variableA, variableB]
}
exports.myfunction = myfunction;
然后我尝试在另一个文件中使用它
var helper = require('./helper.js');
var start = function(a,b){
....
const result = await helper.myfunction('test','test');
}
exports.start = start;
我犯了个错误
“等待仅在异步函数中有效”
问题是什么?错误不是指
myfunction
,而是指start
async function start() {
....
const result = await helper.myfunction('test', 'test');
}
//我的函数
常量myfunction=异步函数(x,y){
返回[
x,,
Y
];
}
//启动功能
const start=异步函数(a,b){
const result=wait myfunction('test','test');
控制台日志(结果);
}
//呼叫开始
start()代码>错误不是指myfunction
,而是指start
async function start() {
....
const result = await helper.myfunction('test', 'test');
}
//我的函数
常量myfunction=异步函数(x,y){
返回[
x,,
Y
];
}
//启动功能
const start=异步函数(a,b){
const result=wait myfunction('test','test');
控制台日志(结果);
}
//呼叫开始
start()
要使用等待
,其执行上下文本质上需要是异步
正如上面所说的,您需要定义您的执行上下文的性质,在这里您愿意在任何事情之前等待任务
只需将async
放在执行async
任务的fn
声明之前。
var start = async function(a, b) {
// Your async task will execute with await
await foo()
console.log('I will execute after foo get either resolved/rejected')
}
说明:
在您的问题中,您正在导入一个方法
,该方法本质上是异步的
,将并行执行。但是,您尝试执行async
方法的地方位于不同的执行上下文中,您需要定义async
以使用wait
var helper = require('./helper.js');
var start = async function(a,b){
....
const result = await helper.myfunction('test','test');
}
exports.start = start;
想知道引擎盖下面发生了什么
await
使用承诺/未来/任务返回方法/函数,并且async
将方法/函数标记为能够使用await
另外,如果您熟悉承诺
,等待
实际上也在执行相同的承诺/解决过程。创建承诺链并执行resolve
回调中的下一个任务
有关更多信息,请参阅。要使用等待
,其执行上下文本质上需要是异步
正如上面所说的,您需要定义您的执行上下文的性质,在这里您愿意在任何事情之前等待任务
只需将async
放在执行async
任务的fn
声明之前。
var start = async function(a, b) {
// Your async task will execute with await
await foo()
console.log('I will execute after foo get either resolved/rejected')
}
说明:
在您的问题中,您正在导入一个方法
,该方法本质上是异步的
,将并行执行。但是,您尝试执行async
方法的地方位于不同的执行上下文中,您需要定义async
以使用wait
var helper = require('./helper.js');
var start = async function(a,b){
....
const result = await helper.myfunction('test','test');
}
exports.start = start;
想知道引擎盖下面发生了什么
await
使用承诺/未来/任务返回方法/函数,并且async
将方法/函数标记为能够使用await
另外,如果您熟悉承诺
,等待
实际上也在执行相同的承诺/解决过程。创建承诺链并执行resolve
回调中的下一个任务
有关更多信息,请参阅。当我遇到此错误时,原来我在“async”函数中调用了map函数,因此此错误消息实际上是指没有标记为“async”的map函数。我绕过了这个问题,从map函数中调用了“await”,并提出了一些获得预期行为的其他方法
var myfunction = async function(x,y) {
....
someArray.map(someVariable => { // <- This was the function giving the error
return await someFunction(someVariable);
});
}
var myfunction=异步函数(x,y){
....
someArray.map(someVariable=>{/当我遇到这个错误时,我发现在我的“async”函数中调用了map函数,所以这个错误消息实际上是指没有标记为“async”的map函数。我通过使用“wait”来解决这个问题调用map函数,并想出一些其他方法来获得预期的行为
var myfunction = async function(x,y) {
....
someArray.map(someVariable => { // <- This was the function giving the error
return await someFunction(someVariable);
});
}
var myfunction=异步函数(x,y){
....
someArray.map(someVariable=>{/当前实现的async
/await
只支持async
函数中的await
关键字更改start
函数签名,以便在start
中使用await
var start = async function(a, b) {
}
对于那些感兴趣的人来说,关于顶级await
的建议目前处于第2阶段:当前的async
/await
实现只支持async
函数中的await
关键字更改start
函数签名,以便您可以在函数中使用await
<代码>开始
var start = async function(a, b) {
}
对于那些感兴趣的人来说,关于顶级await
的建议目前正处于第2阶段:是的,await/async是一个很好的概念,但是实现完全失败了
无论出于何种原因,wait关键字的实现使得它只能在异步方法中使用。这实际上是一个bug,尽管除了这里,您不会在任何地方看到它被称为这样的bug。该bug的修复方法是实现wait关键字,使得它只能用于调用异步函数,而不管在什么情况下调用函数本身是同步的或异步的
由于这个错误,如果使用wait调用代码中某个真正的异步函数,那么所有函数都必须标记为异步,并且所有函数调用都必须使用wait
这本质上意味着您必须将承诺的开销添加到整个应用程序中的所有函数中,其中大多数不是也永远不会是异步的
如果你
function helper(callback){
function doA(){
var array = ["a ","b ","c "];
var alphabet = "";
return new Promise(function (resolve, reject) {
array.forEach(function(key,index){
alphabet += key;
if (index == array.length - 1){
resolve(alphabet);
};
});
});
};
function doB(){
var a = "well done!";
return a;
};
async function make() {
var alphabet = await doA();
var appreciate = doB();
callback(alphabet+appreciate);
};
make();
};
helper(function(message){
console.log(message);
});