Created
December 16, 2021 01:37
-
-
Save ryumada/da37945f4bf2bf6e1c138a5b63f66010 to your computer and use it in GitHub Desktop.
differences between synchronous and asynchronous using Promise
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const AWS = require('aws-sdk'); | |
class StorageService { | |
constructor() { | |
this._S3 = new AWS.S3(); | |
} | |
writeFile(file, meta) { | |
const parameter = { | |
Bucket: process.env.AWS_BUCKET_NAME, // Nama S3 bucket yang ingin digunakan | |
Key: +new Date() + meta.filename, // Nama berkas yang akan disimpan | |
Body: file._data, // Berkas (dalam bentuk Buffer) yang akan disimpan | |
ContentType: meta.headers['content-type'], // MIME Type berkas yang akan disimpan | |
}; | |
// GIVE ATTENTION TO THE CODES BELOW ======================================================================== | |
return new Promise((resolve, reject) => { | |
this._S3.upload(parameter, (error, data) => { | |
if(error) { | |
return reject(error); | |
} | |
return resolve(data); | |
}); | |
}); | |
} | |
} | |
module.exports = StorageService; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const AWS = require('aws-sdk'); | |
class StorageService { | |
constructor() { | |
this._S3 = new AWS.S3(); | |
} | |
writeFile(file, meta) { | |
const parameter = { | |
Bucket: process.env.AWS_BUCKET_NAME, // Nama S3 bucket yang ingin digunakan | |
Key: +new Date() + meta.filename, // Nama berkas yang akan disimpan | |
Body: file._data, // Berkas (dalam bentuk Buffer) yang akan disimpan | |
ContentType: meta.headers['content-type'], // MIME Type berkas yang akan disimpan | |
}; | |
// GIVE ATTENTION TO THE CODES BELOW ======================================================================== | |
this._S3.upload(parameter, () => { | |
if(error) { | |
// upload error | |
return; | |
} | |
// upload success | |
}); | |
} | |
} | |
module.exports = StorageService; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment