twit
twit copied to clipboard
postMediaChunked doesn’t return a Promise
Hi, this is my first time I've posted an issue on GitHub, so please let me know if I'm doing something wrong.
TLDR: Feature request: Refactor postMediaChunked
so that it returns a Promise, making it behave inline with the rest of the methods.
In the README, the documentation mentions I can use either callbacks or promises to interact with the API:
Twit has promise support; you can use the callback API, promise API, or both at the same time.
This seems to be the case for most methods except for the postMediaChunked
method. I've tried something like:
const T = new Twit(/* blah */)
const file = path.resolve(__dirname, './myCoolSelfie.jpg')
T.postMediaChunked({ file_path: file }).then(data => {
console.log(data)
})
but I'm getting this error:
TypeError: Cannot read property 'then' of undefined
which leads me to believe that the postMediaChunked
method doesn't return a Promise. This error doesn't occur when I refactor the code to use callbacks instead. Therefore I ask that this method be rewritten or refactored so that it can use both Promises and callbacks, just as stated in the README.
I have observed the same. This needs to be fixed.
@souvikmaji @MindfulMinun this library doesn't unfortunately return a promise in this case due to the way the Twitter API's handle file uploads, but as a workaround you can always wrap the postMediaChunked function call inside a promise
import { tmpdir } from 'os';
import { join } from 'path';
const executeTwitterUpload = async (buffer: Buffer, TwitterConfig: TwitterOAuth): Promise<TwitterMediaFile> => {
return new Promise(async (resolve, reject) => {
try {
const file_path = join(tmpdir(), 'test.jpg');
writeFileSync(file_path, buffer);
TwitterConfig.postMediaChunked({ file_path }, async (error, twitterMediaFile, _response) => {
if (!error) {
try {
resolve(twitterMediaFile as TwitterMediaFile);
} catch (error) {
console.error(error);
reject(error);
}
} else {
reject(error);
}
});
} catch (error) {
console.error(error);
reject(error);
}
});
};