2

How can we use promise on top of Duktape?

My Scenario: Migrating a client side javascript code that loads well on web browsers to Duktape. I am using Dukluv (binding with Duktape and libuv library) binary to run the JavaScript. However my javascript depends on BlueBird library.

When I try to execute I get an error: no async scheduler available

The problem is in bluebird's schedule.js line 33. Code is below for reference:

else if ((typeof MutationObserver !== "undefined" &&
         (_MutationObserver = MutationObserver)) ||
         (typeof WebKitMutationObserver !== "undefined" &&
         (_MutationObserver = WebKitMutationObserver))) {
    schedule = (function() {
        var div = document.createElement("div");
        var queuedFn = void 0;
        var observer = new _MutationObserver(
            function Promise$_Scheduler() {
                var fn = queuedFn;
                queuedFn = void 0;
                fn();
            }
       );
        observer.observe(div, {
            attributes: true
        });
        return function Promise$_Scheduler(fn) {
            queuedFn = fn;
            div.classList.toggle("foo");
        };

    })();
}

There is no webkitmutationobserver nor document object as I am running my script on Dukluv without any browser.

I also tried to use Q library, even there I get an error setTimeout not defined. Please suggest me a solution, if possible with some snippet of code. Thanks

4

1 回答 1

1

Bluebird lets you explicitly set the scheduler for these scenarios exactly. You need to tell bluebird how to execute a function directly:

Promise.setScheduler(function(fn){ // fn is what to execute
    var timer = uv.new_timer.call({});
    uv.timer_start(timer, 0, 0, fn); // add the function as a callback to the timer
});

Do this above all your promise code - this will resolve the above error.

于 2014-12-12T01:05:17.813 回答