简体   繁体   中英

JS await only valid in async function. How to resolve?

Can anyone please tell me I defined a function as async but still it not working and throwing error How can I resolve this issue?

 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);

There are many mistakes in the code. I hope the snippet helps you.

 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.

You cannot use await outside of an async function. It is how the API is designed. See the docs at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await :

The await operator is used to wait for a Promise. It can only be used inside an async function.

If you want to use await you need to wrap

let course_id = await get_course(2);

inside an async function.

Also, as Daniel A. White pointed out, setTimeout doesn't return a Promise , but a timer ID that you can later use to cancel the timeout.

You could use IIFE to wrap your await call in async function but you also need return promise from other function.

 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); })()

setTimeout doesn't return a promise. You have to wrap it in a new Promise(...) call. The get_course(2) call is just a free/floating call that isn't inside of any function, so await wont work there.

You should wrap you setTimeout into Promise. see the example:

 (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); })();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM