我正在使用打字稿和口香糖。
我正在使用以下任务来编译我的打字稿
gulp.task('compile', function () {
return gulp.src('src/**/**/*.*')
.pipe(ts({
"target": "ES5",
"module": "system",
"isolatedModules": true
}))
.pipe(gulp.dest('functions'));
});
打字稿代码是
async welcomeIntent() {
this.app.intent('Default Welcome Intent',(conv,input) => {
conv.ask('<speak>Welcome Intent<break time="1"/> </speak>');
let output;
output = await this.entryService.getTodaysEntryOfUser(conv);
output.then((todaysEntry) => {
let question:Question = this.testService.getStartQuestion(1);
return;
})
.catch(err => {
console.log('Error getting document', err);
});
});
Promise实现了Typescript功能
public getTodaysEntryOfUser(conv: any): Promise<any> {
let documentDetail;
let self = this;
let subjectId = conv.request.user.userId;
let questionID = conv.request.user.questionID;
let docRef= this.dbManager.collection('Question');
console.log("docRef-------------->",docRef);
return new Promise<any>((resolve, reject) => {
docRef.get().then(doc => {
documentDetail = doc;
conv.user.response= doc.data();
console.log('Document data------>', doc.data());
return resolve(doc.data());
})
.catch(err => {
console.log('Error getting document', err);
reject(err);
});
});
}
从打字稿编译为JS后,JS方法的代码为
IntentService.prototype.welcome= function () {
return __awaiter(this, void 0, void 0, function () {
var _this = this;
return __generator(this, function (_a) {
this.app.intent('test', function (conv, input) {
conv.ask('<speak>Welcome Intent<break time="1"/> </speak>');
var output;
output = yield _this.testEntryService.getTodaysEntryOfUser(conv);
output.then(function (todaysEntry) {
var question = _this.testService.getStartQuestion(1);
return;
})["catch"](function (err) {
console.log('Error getting document', err);
});
});
return [2 /*return*/];
});
});
};
Promise实现的功能的生成的JS代码
entryService.prototype.getTodaysEntryOfUser = function (conv) {
var documentDetail;
var self = this;
var subjectId = conv.request.user.userId;
var questionID = conv.request.user.questionID;
var docRef = this.dbManager.collection('Question');
console.log("docRef-------------->", docRef);
return new Promise(function (resolve, reject) {
docRef.get().then(function (doc) {
documentDetail = doc;
conv.user.response = doc.data();
console.log('Document data------>', doc.data());
return resolve(doc.data());
})
.catch(function (err) {
console.log('Error getting document', err);
reject(err);
});
});
};
我遇到的错误是
error Parsing error: The keyword 'yield' is reserved
此处的问题是未对关键字“ Yield”进行评论。如果我使用“ firebase deploy --only functions”进行部署,则会收到错误消息。收益关键字行应如下所示,即,如果我评论收益关键字,则不会出错。
output = /*yield */ _this.entryService.getTodaysEntryOfUser(conv);
如何使用gulp做到这一点?
我引用了链接“ https://basarat.gitbooks.io/typescript/docs/async-await.html”
在此指定,如果我们使用“ ES5”生成,则生成的代码将带有“ Yield”注释。