问题描述
我正在使用JavaScript API,其中大多数功能都是异步的. API是 webkit这是与操作SQLITE3数据库的功能子集的结合.我了解将事物异步的设计决定,以免阻止并提供响应迅速的用户界面.在我的情况下,我知道我对异步API调用的使用将很快执行.由于这种情况,我想为开发人员提供更清洁,更易于使用的包装API,该API强迫同步通话.
这是异步调用
db.executeSql(sqlStatement, function(result) { // do something with result });
这就是我想做的
var result = dbWrapper.executeSql(sqlStatement); // do something with result
是否有设计模式/方法?优选的书面或链接到代码示例. Target Platform/Broswer是iPhone上的移动野生动物园.
谢谢
推荐答案
对不起,JavaScript不提供语言原语(例如线程或coroutines)来使异步事物同步或反之亦然.
您通常*仅获取一个执行线程,因此您无法从计时器或XMLHTTPRequest ReadyStateChange获得回调,直到导致创建请求的堆栈已完全解开为止.
>因此,简而言之,您无法真正做到;您链接的WebKit页面上带有嵌套封闭的方法是我知道在这种情况下可读代码的唯一方法.
*:除了在某些晦涩的情况下,这对您没有帮助,通常被视为错误
其他推荐答案
stratifiedjs允许您做到这一点.
甚至还有一篇有关如何将其应用于浏览器存储的文章: http://onilabs.com/blog/blog/stratifying-stratifying-asstratifying-ashratifying-ashonchronous-storoge
这是分层的javascript库,它使用 https://gist.github.com/613526
示例就像:
var db = require("webdatabase").openDatabase("CandyDB", ...); try { var kids = db.executeSql("SELECT * FROM kids").rows; db.executeSql("INSERT INTO kids (name) VALUES (:name);", [kids[0]]); alert("done"); } catch(e) { alert("something went wrong"); }
也许有点晚,但是当时的技术不存在;)
其他推荐答案
您可以尝试以下操作:
function synch() { var done = false; var returnVal = undefined; // asynch takes a callback method // that is called when done asynch(function(data) { returnVal = data; done = true; }); while (done == false) {}; return returnVal; }
,但这可能会在异步方法的持续时间内冻结您的浏览器...
或查看叙述性javascript:叙事JavaScript是JavaScript语言的小扩展,可为异步事件回调提供阻止功能.这使得异步代码令人耳目一新且可理解.
http://neilmix.com/narrmix.com/narrativejs/doc/doc/doc/index.htex.html
Mike
问题描述
I'm working with a JavaScript API where most of the functions are asynchronous. The API is the WebKit JavaScript Database API which is a binding to a subset of functionality to manipulate SQLite3 databases. I understand the design decision to make things async as to not block and provide a responsive user interface. In my situation I know that my usage of the async API calls will execute fast. Since this is the case I'd like to provide my developers a cleaner and easier to use wrapper API that forces synchronous calls.
Here's the async call
db.executeSql(sqlStatement, function(result) { // do something with result });
And here's what I'd like to be able to do
var result = dbWrapper.executeSql(sqlStatement); // do something with result
Is there a design pattern/way to do this? A written or linked to code example is preferred. The target platform/broswer is Mobile Safari on the iPhone.
Thank you
推荐答案
Sorry, JavaScript does not provide the language primitives (eg. threads or coroutines) to make asynchronous things act synchronously or vice-versa.
You generally* get one thread of execution only, so you can't get a callback from a timer or XMLHttpRequest readystatechange until the stack of calls leading to the creation of the request has completely unravelled.
So in short, you can't really do it; the approach with nested closures on the WebKit page you linked is the only way I know of to make the code readable in this situation.
*: except in some obscure situations which wouldn't help you and are generally considered bugs
其他推荐答案
StratifiedJS allows you to do exactly that.
There's even an article on how to apply it on browser storage: http://onilabs.com/blog/stratifying-asynchronous-storage
And this is the Stratified JavaScript library it uses https://gist.github.com/613526
The example goes like:
var db = require("webdatabase").openDatabase("CandyDB", ...); try { var kids = db.executeSql("SELECT * FROM kids").rows; db.executeSql("INSERT INTO kids (name) VALUES (:name);", [kids[0]]); alert("done"); } catch(e) { alert("something went wrong"); }
maybe a bit late, but the tech didn't exist back then ;)
其他推荐答案
You can try something like:
function synch() { var done = false; var returnVal = undefined; // asynch takes a callback method // that is called when done asynch(function(data) { returnVal = data; done = true; }); while (done == false) {}; return returnVal; }
But that may freeze your browser for the duration of the asynch method...
Or take a look at Narrative JavaScript: Narrative JavaScript is a small extension to the JavaScript language that enables blocking capabilities for asynchronous event callbacks. This makes asynchronous code refreshingly readable and comprehensible.
http://neilmix.com/narrativejs/doc/index.html
Mike