Promises: Managing multiple Deferred instances in a Service

timwjohn's Avatar

timwjohn

21 Jun, 2013 12:56 PM

Hi all,

I have an SQLService that utilises SQLRunner and essentially wraps the execute() method that its subclasses can use. This command may be called to perform many asynchronous queries simultaneously.

I'm really keen to implement the Promises pattern with promise-as3 (is it a pattern?) for this, and this would be simple if I only had one query at a time to worry about. However, my service will have to create and track an array of Deferred instances. So the question is - how?

When one of the active statements completes (successfully or otherwise), what can I do to pluck it's associated Deferred instance from the array? Ideally I'd like a unique identifier that links the execution of a statement to its completion.

Cheers!

  1. Support Staff 1 Posted by creynders on 21 Jun, 2013 02:01 PM

    creynders's Avatar

    I don't know the air-sqlite lib, but at a first glance I'd say all you need to do is to pass a function closure as the result handler and resolve the Deferred from there. Something like this: (pseudo-ish code)

    function loadFoo():Promise{
        var deferred:Deferred = new Deferred();
        var handler : Function = function(result){
            deferred.resolve(result);
        }
        sqlRunner.execute(LOAD_FOO_SQL, {}, handler);
        return deferred.promise;
    }
    
  2. 2 Posted by Paul Robertson on 21 Jun, 2013 05:25 PM

    Paul Robertson's Avatar

    Unfortunately I only know the air-sqlite lib and not the as3-promises
    one, but at a glance creyenders's suggestion looks like it should work.

    If it's not working, or if you have other questions about the SQLRunner
    ("air-sqlite") lib, feel free to email me ([email blocked]). Not to
    say that you can't keep asking questions here -- just that I don't want
    to inadvertently turn the Robotlegs forum into a support forum for my
    library =)

    Paul

  3. Support Staff 3 Posted by Ondina D.F. on 21 Jun, 2013 05:57 PM

    Ondina D.F.'s Avatar

    Not to say that you can't keep asking questions here -- just that I don't want to inadvertently turn the Robotlegs forum into a support forum for my
    library =)

    Paul, I’m sure you won’t abuse this forum :) I don’t see a problem with you answering questions about air-sqlite and your lib, if that’s helping rl-users solve their problems.
    There have been lots of questions on the forum having almost nothing to do with rl, even though we’ve tried to answer them.

    Btw, please come to our party!
    http://knowledge.robotlegs.org/discussions/problems/2516-let-the-ro...

    Ondina

  4. 4 Posted by timwjohn on 22 Jun, 2013 12:13 PM

    timwjohn's Avatar

    Yeah I realised that this wasn't exactly a RL question! But as Paul is a Robotlegger and Shaun was involved in the promises-as3 library I figured this was a good place to ask.

    creynders - of course, function closures are the answer. I've always subconsciously avoided them in AS3 for some reason, though use them all the time in JS. It's working great, thanks!

    Paul, thank you also. air-sqlite has also been a big help.

    Right, back to the party ;)

  5. Support Staff 5 Posted by creynders on 22 Jun, 2013 12:22 PM

    creynders's Avatar

    Glad I could help! Don't worry about asking questions about promises on the
    forum, they almost ended up being baked into RL, so we're pretty familiar
    with them.

  6. Support Staff 6 Posted by Ondina D.F. on 22 Jun, 2013 12:38 PM

    Ondina D.F.'s Avatar

    Yeah I realised that this wasn't exactly a RL question! But as Paul is a Robotlegger and Shaun was involved in the promises-as3 library I figured this was a good place to ask.

    But that's what I meant. It's absolutely ok to ask such questions :)

  7. 7 Posted by timwjohn on 22 Jun, 2013 02:50 PM

    timwjohn's Avatar

    That's good to know, as I probably will again!

    Thanks again guys.

  8. Support Staff 8 Posted by creynders on 22 Jun, 2013 05:19 PM

    creynders's Avatar

    Closing this for now, feel free to reopen if there's more.

  9. creynders closed this discussion on 22 Jun, 2013 05:19 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac