{"action":"created","issue":{"url":"https://api.github.com/repos/techfort/LokiJS/issues/108","labels_url":"https://api.github.com/repos/techfort/LokiJS/issues/108/labels{/name}","comments_url":"https://api.github.com/repos/techfort/LokiJS/issues/108/comments","events_url":"https://api.github.com/repos/techfort/LokiJS/issues/108/events","html_url":"https://github.com/techfort/LokiJS/issues/108","id":63165026,"number":108,"title":"how to use loaded db outside of callback functions","user":{"login":"tommyhtc","id":4024432,"avatar_url":"https://avatars.githubusercontent.com/u/4024432?v=3","gravatar_id":"","url":"https://api.github.com/users/tommyhtc","html_url":"https://github.com/tommyhtc","followers_url":"https://api.github.com/users/tommyhtc/followers","following_url":"https://api.github.com/users/tommyhtc/following{/other_user}","gists_url":"https://api.github.com/users/tommyhtc/gists{/gist_id}","starred_url":"https://api.github.com/users/tommyhtc/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tommyhtc/subscriptions","organizations_url":"https://api.github.com/users/tommyhtc/orgs","repos_url":"https://api.github.com/users/tommyhtc/repos","events_url":"https://api.github.com/users/tommyhtc/events{/privacy}","received_events_url":"https://api.github.com/users/tommyhtc/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":6,"created_at":"2015-03-20T05:21:28Z","updated_at":"2015-03-20T20:44:03Z","closed_at":null,"body":"hi there, so I'm just learning loki,\r\n\r\nlet's say I have a loki database called \"test.json\" with a collection \"Users\".\r\nI'm running node,\r\nIn some .js file, I want to access that database and write to it.\r\n\r\nCurrently, I only know how to access a loaded database with callbacks,\r\nfor example, with:\r\n\r\ndb = new loki('test.json',{autoload:true, autocallback: foo()});\r\n\r\nand then I'm able to access the loaded db in foo(), where I can insert stuff or get stuff, etc.\r\n\r\nHowever, is there a way to access the loaded database outside of the callback, or without using callback??\r\n\r\nfor example,\r\nfoo(){\r\n   db = new loki('test.json',{autoload:true}\r\n   somefield = db.getCollection(\"Users\").get(1);\r\n}\r\n\r\nis this possible??\r\nI've tried loadDatabase but it also requires a callback..\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/techfort/LokiJS/issues/comments/84139840","html_url":"https://github.com/techfort/LokiJS/issues/108#issuecomment-84139840","issue_url":"https://api.github.com/repos/techfort/LokiJS/issues/108","id":84139840,"user":{"login":"techfort","id":4130173,"avatar_url":"https://avatars.githubusercontent.com/u/4130173?v=3","gravatar_id":"","url":"https://api.github.com/users/techfort","html_url":"https://github.com/techfort","followers_url":"https://api.github.com/users/techfort/followers","following_url":"https://api.github.com/users/techfort/following{/other_user}","gists_url":"https://api.github.com/users/techfort/gists{/gist_id}","starred_url":"https://api.github.com/users/techfort/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/techfort/subscriptions","organizations_url":"https://api.github.com/users/techfort/orgs","repos_url":"https://api.github.com/users/techfort/repos","events_url":"https://api.github.com/users/techfort/events{/privacy}","received_events_url":"https://api.github.com/users/techfort/received_events","type":"User","site_admin":false},"created_at":"2015-03-20T20:44:03Z","updated_at":"2015-03-20T20:44:03Z","body":"@tommyhtc ok - let's take a step back. JS is single-threaded. This means things cannot EVER happen concurrently (academically speaking this does not even happen in languages that only support threading, but only in those supporting concurrency, but that's a whole other story). \r\nSo, talking about normal code, if you have:\r\n```javascript\r\nfunction a() { return 1; }\r\nfunction b() { return 2; }\r\nfunction c() { return 3; }\r\n// and call\r\na();\r\nb();\r\nc();\r\n```\r\nthey will execute in order, no matter what.\r\nThe asynchronicity problem enters when you deal with one of these:\r\n* I/O (including network commuincations, e.g. ajax, or http requests on server-side)\r\n* you use `setInterval` or `setTimeout`\r\n\r\nAsync in javascript means this (roughly): as soon as the existing sync code has been executed, if the processor is free to do other things, then i'll execute async.\r\nFor example, you may set a timeout for a function to execute after 1ms, let's say prompt an alert. The you also stick in a function that does heavy processing, like this:\r\n```javascript\r\nsetTimeout(function () { alert('hello'); }, 1);\r\nheavyProcessingStuff();\r\n```\r\nYou instruct to execute that alert after 1ms, but heavyProcessingStuff() kicks in before that, so now the processor has to wait for heavyProcessingStuff() to be done before it can execute it. And if heavyProcessingStuff() takes 35 minutes, well that's what you're going to have to wait.\r\n\r\nIn conclusion: LokiJS adopts a synchronous style of programming, except for when it can't due to node.js's own event-loop, namely disk or network I/O. The only circumstance when you will need to pay attention to callbacks are loads and saves to disk, and - if you do it- syncing to remote servers through requests (like ajax or server-side http() ). Other than that ... you're good."}}