Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problems retrieving maillboxes object when listing mailboxes #98

Closed
reeseovine opened this issue May 16, 2016 · 5 comments
Closed

Problems retrieving maillboxes object when listing mailboxes #98

reeseovine opened this issue May 16, 2016 · 5 comments

Comments

@reeseovine
Copy link

reeseovine commented May 16, 2016

Hi, I'm trying to use the listMailboxes() method of the imap client. Here is what I'm trying to do:

client = new ImapClient(host, port, options);
client.connect().then(() => {
    client.listMailboxes().then((mailboxes) => {
        console.dir(mailboxes);
    });
    client.close();
});

It runs fine, outputs the server responses in the terminal saying I've connected and it's listing mailboxes, and closes up without errors. The problem I'm having is that I can't seem to actually use the mailboxes object once it has gotten a response from the server.

It may be because of this notation in the readme:

client.listMailboxes().then((mailboxes) => { ... });

I am really unfamiliar with what's going on here. I understand then() is supposed to run a function once it's done, but I have never seen the () => { ... } notation before. It looks like it may just be shorthand for function(){ } but I'm just not used to that. Maybe that isn't what's causing the problems for me.

@reeseovine
Copy link
Author

Can someone please help me out here?

@marcus433
Copy link

marcus433 commented May 17, 2016

I believe the problem you are having is the client is closing before the response is returned.
As BrowserBox is asynchronous the close function is actually called before the listing is completed. The following code should work.

client = new ImapClient(host, port, options);
client.connect()
.then(client.listMailboxes)
.then((mailboxes) => {
        console.dir(mailboxes);
 })
.then(client.close);

@asutherland
Copy link
Contributor

nit: Note that unless ImapClient is now automatically binding its methods, you'd need to do something like client.listMailboxes.bind(client) and client.close.bind(client) or wrap them in arrow-functions too like () => client.listMailboxes() and () => client.close. (Note that it's important to not wrap the arrow function body in braces if you want the implicit return of the Promise returned by listMailboxes.

@marcus433
Copy link

Good catch @asutherland :-)

@reeseovine
Copy link
Author

listMailboxes and close both need parentheses to run the functions, but with the two of your suggestions it works! Thank you so much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants