admin管理员组文章数量:1023744
I'm getting unexpected identifier when i use async or await in nodejs. I'm on node version 8.5.0. Completely blocked on this. Is there anyway to fix this?
async function methodA(options) {
rp(options)
.then(function (body) {
serviceClusterData = JSON.parse(body);
console.log("Step 2");
console.log("Getting cluster details from zookeeper");
})
.catch(function (err) {
console.log("Get failed!");
});
}
await methodA(options);
console.log("Step 3!");
Tried this after first answer :
var serviceClusterData = "";
console.log("Step 1!");
////////////////////
async function methodA(options) {
await rp(options)
.then(function (body) {
serviceClusterData = JSON.parse(body);
console.log("Step 2");
console.log("Getting cluster details from zookeeper");
})
.catch(function (err) {
console.log("Get failed!");
});
}
methodA(options);
console.log("whoops Step 3!");
Still gets out of order :( Step 1 Step 3 Step 2
I'm getting unexpected identifier when i use async or await in nodejs. I'm on node version 8.5.0. Completely blocked on this. Is there anyway to fix this?
async function methodA(options) {
rp(options)
.then(function (body) {
serviceClusterData = JSON.parse(body);
console.log("Step 2");
console.log("Getting cluster details from zookeeper");
})
.catch(function (err) {
console.log("Get failed!");
});
}
await methodA(options);
console.log("Step 3!");
Tried this after first answer :
var serviceClusterData = "";
console.log("Step 1!");
////////////////////
async function methodA(options) {
await rp(options)
.then(function (body) {
serviceClusterData = JSON.parse(body);
console.log("Step 2");
console.log("Getting cluster details from zookeeper");
})
.catch(function (err) {
console.log("Get failed!");
});
}
methodA(options);
console.log("whoops Step 3!");
Still gets out of order :( Step 1 Step 3 Step 2
Share Improve this question edited Nov 15, 2017 at 20:45 Michał Perłakowski 92.9k30 gold badges163 silver badges188 bronze badges asked Sep 20, 2017 at 9:42 user461112user461112 4,1713 gold badges22 silver badges25 bronze badges 3- Yes there is a way: show us your code. – TGrif Commented Sep 20, 2017 at 10:13
- Updated the question with code. Thanks – user461112 Commented Sep 20, 2017 at 10:19
- Possible duplicate of 'await Unexpected identifier' on Node.js 7.5 – Michał Perłakowski Commented Nov 15, 2017 at 20:45
2 Answers
Reset to default 5You can't use await outside of an async function.
async function methodA(options) {
await rp(options)
.then(function (body) {
serviceClusterData = JSON.parse(body);
console.log("Step 2");
console.log("Getting cluster details from zookeeper");
})
.catch(function (err) {
console.log("Get failed!");
});
}
methodA(options);
console.log("Step 3!");
'use strict'
function methodA(options) {
return new Promise(resolve => {
setTimeout(() => {
console.log(1)
resolve(true);
}, 2000);
})
}
//Sync Declartion
async function test() {
//Await declaration
await methodA({});
console.log(2);
}
test();
It seems there's is some syntax error in your code. Above code works in 8.5.0
Reference https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Statements/async_function
I'm getting unexpected identifier when i use async or await in nodejs. I'm on node version 8.5.0. Completely blocked on this. Is there anyway to fix this?
async function methodA(options) {
rp(options)
.then(function (body) {
serviceClusterData = JSON.parse(body);
console.log("Step 2");
console.log("Getting cluster details from zookeeper");
})
.catch(function (err) {
console.log("Get failed!");
});
}
await methodA(options);
console.log("Step 3!");
Tried this after first answer :
var serviceClusterData = "";
console.log("Step 1!");
////////////////////
async function methodA(options) {
await rp(options)
.then(function (body) {
serviceClusterData = JSON.parse(body);
console.log("Step 2");
console.log("Getting cluster details from zookeeper");
})
.catch(function (err) {
console.log("Get failed!");
});
}
methodA(options);
console.log("whoops Step 3!");
Still gets out of order :( Step 1 Step 3 Step 2
I'm getting unexpected identifier when i use async or await in nodejs. I'm on node version 8.5.0. Completely blocked on this. Is there anyway to fix this?
async function methodA(options) {
rp(options)
.then(function (body) {
serviceClusterData = JSON.parse(body);
console.log("Step 2");
console.log("Getting cluster details from zookeeper");
})
.catch(function (err) {
console.log("Get failed!");
});
}
await methodA(options);
console.log("Step 3!");
Tried this after first answer :
var serviceClusterData = "";
console.log("Step 1!");
////////////////////
async function methodA(options) {
await rp(options)
.then(function (body) {
serviceClusterData = JSON.parse(body);
console.log("Step 2");
console.log("Getting cluster details from zookeeper");
})
.catch(function (err) {
console.log("Get failed!");
});
}
methodA(options);
console.log("whoops Step 3!");
Still gets out of order :( Step 1 Step 3 Step 2
Share Improve this question edited Nov 15, 2017 at 20:45 Michał Perłakowski 92.9k30 gold badges163 silver badges188 bronze badges asked Sep 20, 2017 at 9:42 user461112user461112 4,1713 gold badges22 silver badges25 bronze badges 3- Yes there is a way: show us your code. – TGrif Commented Sep 20, 2017 at 10:13
- Updated the question with code. Thanks – user461112 Commented Sep 20, 2017 at 10:19
- Possible duplicate of 'await Unexpected identifier' on Node.js 7.5 – Michał Perłakowski Commented Nov 15, 2017 at 20:45
2 Answers
Reset to default 5You can't use await outside of an async function.
async function methodA(options) {
await rp(options)
.then(function (body) {
serviceClusterData = JSON.parse(body);
console.log("Step 2");
console.log("Getting cluster details from zookeeper");
})
.catch(function (err) {
console.log("Get failed!");
});
}
methodA(options);
console.log("Step 3!");
'use strict'
function methodA(options) {
return new Promise(resolve => {
setTimeout(() => {
console.log(1)
resolve(true);
}, 2000);
})
}
//Sync Declartion
async function test() {
//Await declaration
await methodA({});
console.log(2);
}
test();
It seems there's is some syntax error in your code. Above code works in 8.5.0
Reference https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Statements/async_function
本文标签: javascriptunexpected identifier when using awaitasync in nodejsStack Overflow
版权声明:本文标题:javascript - unexpected identifier when using awaitasync in nodejs - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745602899a2158561.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论