谁能告诉我我将一个函数定义为异步函数,但仍然无法正常工作并引发错误。我该如何解决此问题?
var courses = [
{ID: 1, Name: 'course 1', Author: 'Mariela Mckinney', Website: 'www.loarpharei.com'},
{ID: 2, Name: 'course 2', Author: 'Brylee Becker', Website: 'www.phattiodye.com'},
{ID: 3, Name: 'course 3', Author: 'Weston Edwards', Website: 'www.pretathlew.com'},
{ID: 4, Name: 'course 4', Author: 'Nathanael Fox', Website: 'www.senfredral.com'},
{ID: 5, Name: 'course 5', Author: 'Aarav Park', Website: 'www.ebreanerin.com'}
]
async function get_course(ID){
return setTimeout(function(){
console.log('I am fired');
return get_index(ID);
}, 2000);
}
let course_id = await get_course(2);
console.log(course_id);
答案 0 :(得分:2)
代码中有很多错误。希望代码段对您有所帮助。
const courses = [
{id: 1, name: 'course 1', author: 'Mariela Mckinney', website: 'www.loarpharei.com'},
{id: 2, name: 'course 2', author: 'Brylee Becker', website: 'www.phattiodye.com'},
{id: 3, name: 'course 3', author: 'Weston Edwards', website: 'www.pretathlew.com'},
{id: 4, name: 'course 4', author: 'Nathanael Fox', website: 'www.senfredral.com'},
{id: 5, name: 'course 5', author: 'Aarav Park', website: 'www.ebreanerin.com'},
]
// The "async" keyword is actually redundant if there is no "await" keyword in
// the function.
async function getCourse(id) {
return new Promise((resolve) => {
setTimeout(() => {
console.log('I am fired');
resolve(courses[id]);
}, 2000);
});
}
async function main() {
// You can always "await" an Async function, or a function returning a Promise.
const course = await getCourse(2);
console.log(course);
}
main();
// Notes:
// 1. Always use camelCase for field names, function names and variable names.
// 2. Most JavaScript developers prefer 2-space indent, instead of 4.
答案 1 :(得分:1)
您不能在await
函数之外使用async
。 API的设计方式。请参阅https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await上的文档:
await运算符用于等待Promise。它只能在异步函数中使用。
如果要使用await
,则需要包装
let course_id = await get_course(2);
在async
函数内部。
而且,正如丹尼尔·A·怀特(Daniel A. White)所指出的那样,setTimeout
不会返回Promise
,而是一个计时器ID,您以后可以使用它取消超时。
答案 2 :(得分:1)
您可以使用IIFE将等待调用包装在async
函数中,但还需要其他函数的返回承诺。
var courses = [{ID: 1, Name: 'course 1', Author: 'Mariela Mckinney', Website: 'www.loarpharei.com'},{ID: 2, Name: 'course 2', Author: 'Brylee Becker', Website: 'www.phattiodye.com'},{ID: 3, Name: 'course 3', Author: 'Weston Edwards', Website: 'www.pretathlew.com'},{ID: 4, Name: 'course 4', Author: 'Nathanael Fox', Website: 'www.senfredral.com'},{ID: 5, Name: 'course 5', Author: 'Aarav Park', Website: 'www.ebreanerin.com'}]
function get_course(ID) {
console.log('I am fired.')
return new Promise(resolve => {
setTimeout(() => {
resolve(courses)
}, 2000)
})
}
(async() => {
let course_id = await get_course(2);
console.log(course_id);
})()
答案 3 :(得分:0)
setTimeout
不返回承诺。您必须将其包装在new Promise(...)
调用中。 get_course(2)
只是一个自由浮动的调用,它不在任何函数中,因此await
不会在其中起作用。
答案 4 :(得分:0)
您应该将setTimeout包装到Promise中。参见示例:
(async() => {var courses = [
{ID: 1, Name: 'course 1', Author: 'Mariela Mckinney', Website: 'www.loarpharei.com'},
{ID: 2, Name: 'course 2', Author: 'Brylee Becker', Website: 'www.phattiodye.com'},
{ID: 3, Name: 'course 3', Author: 'Weston Edwards', Website: 'www.pretathlew.com'},
{ID: 4, Name: 'course 4', Author: 'Nathanael Fox', Website: 'www.senfredral.com'},
{ID: 5, Name: 'course 5', Author: 'Aarav Park', Website: 'www.ebreanerin.com'}
]
function get_index(id) {
return courses.findIndex(i => i.ID === id);
}
function get_course(ID){
return new Promise(resolve => {
setTimeout(function(){
console.log('I am fired');
resolve(get_index(ID));
}, 2000);
});
}
let course_id = await get_course(2);
console.log(course_id);
})();