Node.js meteor.js fibers and loop

478 views Asked by At

Okay i am learning fibers and i dont know how to implement the "waiting for end of the loop inside the fiber"

Now i have this code what works without problem.

        if (Meteor.isServer) {

        function checkIfIOwnThisItem(callback) {
           setTimeout(function() {
                callback("this callback");
            }, 500);
        }           

        var f = Fiber(function() {
            var fiber = Fiber.current;

            checkIfIOwnThisItem(function(str) {
                fiber.run(str);
            });

            str = Fiber.yield();
            console.log(str);
        });

        f.run();

    }

And i need to replace the setTimeout ...FOR...

        _.each(myBank.items,function(loopItem,key,list){
        if (loopItem.itemId == item.itemId) {
            ownItem = true;
            countOfOwnItemInBank = parseInt(loopItem.number);
        }
    }); 

The whole point is to first check if i already OWN ITEM (own item is true) AND THEN i can do whatever hell i want but i need to be sure the ownItem is true

2

There are 2 answers

9
Jim Mack On BEST ANSWER

It sounds like you are starting from some memories about fibers/threads in desktop programming and trying to map an exact equivalence. I remember that world. Don't think that way. Fibers are more about not waiting around for an answer that could come any time.

I bet if you take smaller steps, think less, and just get some HTML/js to do what you want, then come back here when you hit a stopping block, you will make progress.

Ownership of items, for example, is commonly expressed by a field in a document, ownerid, matching current userid

1
Denis On
Fiber(function() {
  _.each(myBank.items,function(loopItem,key,list){
    if (loopItem.itemId == item.itemId) {
        ownItem = true;
        countOfOwnItemInBank = parseInt(loopItem.number);
    }
  }); 
}).run();

This code should work synchronous.