简体   繁体   中英

Node.js meteor.js fibers and loop

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM