我有以下实验代码:
//JS File which exports
function consoletest(){
console.log("HelloRequire!")
}
function commonAJAXCall(){
return $.get('https://jsonplaceholder.typicode.com/todos/1', {
}).then((response) => {
response = JSON.stringify(response)
console.log(response)
console.log("AJAX happened")
return response
})
}
module.exports = {
doStuff: async () => {
//await commonAJAXCall()
consoletest()
}
}
//JS File which imports
let eventListeners = require('./lib/eCommerceLogic')
$("#AJAXproductnames").on("click", function(){
eventListeners.doStuff()
})
现在我从2019年3月开始关注这个非常棒的教程,内容涉及如何使用async / await和module.exports: https://www.youtube.com/watch?v=YGHnvrDGmJw
但是由于某种原因,它对我不起作用:/ 当我有
doStuff: async () => {
//await commonAJAXCall()
consoletest()
}
使用了异步关键字,出现错误:
Type Error: "exports" is read-only
当我删除它时,一切正常,因此错误肯定与async关键字有关。
我的代码位于ZURB Foundation 6.4项目中,因此有webpack4,babel7和gulp在起作用。我不知道这是否会导致错误,因为在本教程中,一切正常,我只是不明白为什么它不适合我:/
异步/等待肯定可用,我已经花了一天的时间配置babel7才能使用此ES7功能^^