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

Suggest using promises instead of callbacks #1

Open
MarquisdeGeek opened this issue Dec 5, 2016 · 5 comments
Open

Suggest using promises instead of callbacks #1

MarquisdeGeek opened this issue Dec 5, 2016 · 5 comments
Assignees

Comments

@MarquisdeGeek
Copy link

I find callbacks become unwieldy when more than a couple are nested, so I'd be tempted to wrap methods in promises. e.g.

     function whoAmIWithPromises() {
        var d = $.Deferred();

        Ipseorama.whoAmI(function(myfingerprint) {
            d.resolve(myfingerprint);
        }, function(err) {
            d.reject(err);
        });

        return d.promise();
    }

With the equivalent calling code reading:

whoAmIWithPromises().done(function(myfingerprint) {
   gotFinger(myfingerprint);
}).fail(function(errorCase) {
   console.log("could not create identity " + errorCase);
});
@steely-glint
Copy link
Member

Yep - Noted. I'm working on it.
The only place this becomes a tad messy is the interactions with indexDB - where there isn't a proper promise based API.

@steely-glint steely-glint self-assigned this Mar 1, 2017
@MarquisdeGeek
Copy link
Author

Does https://github.com/jakearchibald/idb suffice?

@steely-glint
Copy link
Member

Looks interesting - I'll give it a whirl.

@steely-glint
Copy link
Member

I'm having a shot at that - but (especially on firefox) I'm finding that it isn't easy to work around the fact that promises aren't microtasks - so ideally I'd do a
return db.transaction(that.dbStoreName).objectStore(that.dbStoreName).get('me');
and in the then() check for null, if it is null, I'd asynchronously create a new cert and store it.
Unfortunately that seems to create a problem in firefox, resulting in a vanishing entry and a message
InvalidStateError: An attempt was made to use an object that is not, or is no longer, usable.

I'm still experimenting, but would welcome feedback.

@MarquisdeGeek
Copy link
Author

Ah - not tried FF in a while. I'll have to have a play and see what I find.

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

2 participants