-1

I am trying to build "event" queue similar to this: https://stackoverflow.com/a/24932757/6385482

but without using any libraries (e.g. jQuery, Bluebird, Q)

Example: I have an array

var event = [func1, func2, func3];

...and i want to implement function called 'each' which itaretes 'event' and calls async function sequentially.

Sorry for my English.

Thank you for your help.

Community
  • 1
  • 1
zed
  • 79
  • 7
  • 1
    An example of what you wish to happen might be useful. – Horia Coman May 26 '16 at 10:07
  • This question is **really** unclear. What are `func1`, `func2`, and `func3`? Are they already async, or are they sync and needing a wrapper? Do you want to wait to call `func2` until `func1`'s work is complete, or can they overlap? What have you looked at so far, what's worked, what hasn't worked? – T.J. Crowder May 26 '16 at 10:09

1 Answers1

0

thanks for your reply. Functions are already async, and I need to call func2 after func1 is done (they can't overlap)... I wrote my this function using Bluebird, here is an example:

var Bluebird = Promise.noConflict();

var items = [
    function () {
        return new Bluebird(function (resolve, reject) {
            console.log("promise1 pending");
            setTimeout(function () {
                console.log("promise1 fulfilled");
                resolve();
            }, 1000)
        })
    },
    function () {
        return new Bluebird(function (resolve, reject) {
            console.log("promise2 pending");
            setTimeout(function () {
                console.log("promise2 fulfilled");
                resolve();
            }, 500)
        })
    },
];

Bluebird.each(items, function (item, i) {
    return item();
});

This does exactly what I need, first calls "promise1 pending", then "promise1 fulfilled", then "promise2 pending" and "promise2 fulfilled"...

I tried to write this function with native Prosmises, but I couldn't figure out how.

Thank you for your help

zed
  • 79
  • 7