所以,这是代码
let x = await this.storeFileName(fileName);
所以,我已经将storeFileName
函数声明为异步,我也回复了一个承诺,直到这里的一切都很好。但我收到的错误是:
SyntaxError: Unexpected token this
指向'this'后跟await关键字
顺便说一句我正在使用ES6类,而this关键字指的是该类的对象。
它没有await关键字,但是如果我等待它会抛出错误。
我做错了什么?一切似乎都是正确的。有人可以对我有所启发。
更新:
这是两个功能。
async encodeName(x){
return new Promise((resolve,reject)=>{
const cipher = crypto.createCipher('aes192', this.PASSWORD);
let encrypted = cipher.update(x,'utf8', 'hex');
encrypted += cipher.final('hex');
if(encrypted.length>240){
let x = await this.storeFileName(encrypted);
resolve(`@Fn ${x}`);
}
resolve(encrypted);
});
}
async storeFileName(x){
return new Promise((resolve,reject)=>{
let doc = { encName: x };
filesDb = new db(`${this.mntpnt}/__CORE_rigel.pro/x100.db`);
filesDb.insert(doc,(err,newdoc)=>{
err?reject():resolve(newdoc._id);
});
});
}
顺便说一句,我在node.js
上这样做更新2:
以下是错误消息
A JavaScript error occurred in the main process
Uncaught Exception:
/home/teja/Documents/Rigel/components/diskEncryptor.js:32
let x = await this.storeFileName(encrypted);
^^^^
SyntaxError: Unexpected token this
at createScript (vm.js:53:10)
at Object.runInThisContext (vm.js:95:10)
at Module._compile (module.js:543:28)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.require (module.js:498:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/home/teja/Documents/Rigel/index.js:4:23)
答案 0 :(得分:3)
好的,解释你需要做些什么来使你的函数async
兼容似乎有点棘手。
所以我在这里更新了你的功能,以显示我会做什么。
请注意util.promisify
,现在已将其烘焙到最新版本的Node。
async encodeName(x){
const cipher = crypto.createCipher('aes192', this.PASSWORD);
let encrypted = cipher.update(x,'utf8', 'hex');
encrypted += cipher.final('hex');
if(encrypted.length>240){
let x = await this.storeFileName(encrypted);
return `@Fn ${x}`;
}
return encrypted;
}
async storeFileName(x){
let doc = { encName: x };
filesDb = new db(`${this.mntpnt}/__CORE_rigel.pro/x100.db`);
pinsert = util.promisify(filesDb.insert);
const newdoc = await pinsert(doc);
return newdoc._id;
}
答案 1 :(得分:3)
答案 2 :(得分:2)
您正在创建新功能
return new Promise((resolve,reject)=>{
你应该将该函数声明为async:
return new Promise(async (resolve,reject)=>{
新功能将
async function encodeName(x){
return new Promise(async (resolve,reject)=>{
const cipher = crypto.createCipher('aes192', this.PASSWORD);
let encrypted = cipher.update(x,'utf8', 'hex');
encrypted += cipher.final('hex');
if(encrypted.length>240){
let x = await this.storeFileName(encrypted);
resolve(`@Fn ${x}`);
}
resolve(encrypted);
});
}
答案 3 :(得分:0)
首先非常感谢Keith,Bergi和Maulen帮助我。我拿起了你的代码片段并对它们进行了测试,我终于理解了它背后的概念。我真的不知道谁的答案是正确的,所有你的答案似乎都是正确的。无论如何,谢谢你们,这就是我想出来的。
encodeName(x){
return new Promise(async (resolve,reject)=>{
const cipher = crypto.createCipher('aes192', this.PASSWORD);
let encrypted = cipher.update(x,'utf8', 'hex');
encrypted += cipher.final('hex');
if(encrypted.length>240){
let x = await this.storeFileName(encrypted);
resolve(`@Fn ${x}`);
}
resolve(encrypted);
});
}
storeFileName(x,mntpnt){
return new Promise((resolve,reject)=>{
let doc = { encName: x };
filesDb = new db(`${this.mntpnt}/__CORE_rigel.pro/x100.db`);
filesDb.insert(doc,(err,newdoc)=>{
err?reject():resolve(newdoc._id);
});
});
}