I am too naive in the JS world. Why is my Javascript Promise not working as expected? I thought console.log("update configs");
won't be executed until console.log("checking bucket");
is completed. Thanks in advance for any help.
Note: the 'checking bucket' function continuous check S3 bucket exists every few seconds until the S3 bucket is created.
process.env.AWS_PROFILE = 'aquarius';
process.env.AWS_SDK_LOAD_CONFIG = 'true';
var AWS = require('aws-sdk');
AWS.config.update({region: 'us-east-1'});
// Create S3 service object
s3 = new AWS.S3({apiVersion: '2006-03-01'});
let bucketName = "mybucket.test.com"
// Create S3 service object
var s3 = new AWS.S3({
apiVersion: '2006-03-01'
});
var promise = new Promise(function(resolve, reject) {
setTimeout(function() {
resolve('hello world');
}, 1);
});
promise.then(function(data) {
var params = {
Bucket: bucketName,
ACL: "public-read"
};
s3.createBucket(params, function(err, data) {
console.log("creating bucket");
if (err) {
console.log(err, err.stack); // an error occurred
} else {
console.log(data); // successful
}
});
})
.then(function(data) {
console.log("checking bucket");
var params = {
Bucket: bucketName
};
s3.waitFor('bucketExists', params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log("bucket exist"); // successful response
});
})
.then(function(data) {
console.log("update configs");
var params2 = {
Bucket: bucketName,
ContentMD5: "",
WebsiteConfiguration: {
ErrorDocument: {
Key: "error.html"
},
IndexDocument: {
Suffix: "index.html"
}
}
};
s3.putBucketWebsite(params2, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful
});
});