Conversation
| }); | ||
|
|
||
| return ig.account.login(answers.username, answers.password); | ||
| return Bluebird.try(() => ig.account.login(answers.username, answers.password)).catch( |
There was a problem hiding this comment.
Is Bluebird really necessary for this ? A regular try/catch block should do the work
There was a problem hiding this comment.
I used the code which is an example on the Instagram-private-api, but I didn't ask myself the question of whether Bluebird was really useful or not, I'm going to do some testing and see if it changes anything
There was a problem hiding this comment.
After re-examining the code, Bluebird is used to do try/catch with Promises so a classic try/catch block would not allow you to do that, especially since it catches the error in the catch but also a class "IgLoginTwoFactorRequiredError" which is used to type the error or something like that, from what I understood
There was a problem hiding this comment.
This should be strictly equivalent.
try {
// return await within an async makes the function try to resolve the promise, if not it will fall in the following catch block
return await ig.account.login(answers.username, answers.password)
} catch (err) {
// Only handle IgLoginTwoFactorRequiredError
if (!(err instanceof IgLoginTwoFactorRequiredError)) {
throw e; // Unhandled error
}
// err should now be inferred as IgLoginTwoFactorRequiredError
const { username, totp_two_factor_on, two_factor_identifier } = err.response.body.two_factor_info
const verificationMethod = totp_two_factor_on ? '0' : '1';
const { code } = await inquirer.prompt([
{
type: 'input',
name: 'code',
message: `Enter code received via ${verificationMethod === '1' ? 'SMS' : 'TOTP'}`,
},
]);
try {
// Same thing as above, try to resolve the promise.
return await ig.account.twoFactorLogin({
username,
verificationCode: code,
twoFactorIdentifier: two_factor_identifier,
verificationMethod, // '1' = SMS (default), '0' = TOTP (google auth for example)
trustThisDevice: '1', // Can be omitted as '1' is used by default
});
} catch (e) {
// Log the error. Ideally, it should be rethrown because catching without rethrowing makes the process run as if there was no error
console.error(e)
}
}Here is the complete explanation of return await https://stackoverflow.com/a/62819622/8886385
There was a problem hiding this comment.
so good, you found what was missing, will you update the github repo?
noook
left a comment
There was a problem hiding this comment.
LGTM, except for Bluebird I don't believe this dependency is necessary
No description provided.