0
我正在嘗試編寫一個ember-cli-deploy插件,並且可以真正使用一些有關promise的幫助。在我的index.js主插件,我有以下代碼 index.js:Ember CLI中的承諾/ Ember.js
prepare: function(context) {
...
...
var awsDeploymentOptions = {....};
this._awsCodeDeployClient = new CodeDeployClient({awsDeploymentOptions: awsDeploymentOptions});
}
upload: function() {
...
...
var uploadPromise = (awsDeploymentOptions.revision.revisionType === 'S3') ? this._awsS3Client.upload(filesToUpload, this.readConfig('s3UploadOptions')) : new Promise().resolve();
return uploadPromise.then(function(result){return this._awsCodeDeployClient.createDeployment(result)}.bind(this));
}
預期和承諾得到妥善解決上述作品。
如果我上面的代碼更改爲:
return uploadPromise.then(this._awsCodeDeployClient.createDeployment);
,代碼失敗。然後,我嘗試以下,這也將失敗:
return uploadPromise.then(this._awsCodeDeployClient.createDeployment.bind(this));
在上述兩種情況下,它抱怨未定義的變量/ createDeployment方法,其被定義爲下面的內部屬性:
createDeployment: function(s3FileUploadOptions) {
return new Promise(function(resolve, reject) {
//This is where the problem lies. this is never resolved
//to this module's 'this' and I cannot access this.deploymentOptions
//Any reference to 'this' variable causes an error
var awsDeploymentOptions = this.awsDeploymentOptions;
this.codeDeploy.createDeployment(this.awsDeploymentOptions, function(error, data) {
if (error)
reject(error); // an error occurred
else resolve({awsDeploymentId:data.deploymentId}); // successful response. Return deployment Id
}.bind(this));
}.bind(this));
}
什麼我在上面的兩個場景中做錯了嗎?
您能否請:a)正確縮進您的代碼,並且b)將其縮小到與您的問題相關的部分?我很確定所有AWS代碼都是無關的噪音。 – Tomalak
試試這個。 var that = this;返回uploadPromise.then(this._awsCodeDeployClient.createDeployment.bind(that)); – blessenm