Pattern for wrapping an Asynchronous JavaScript function to make it synchronous

I'm working with a JavaScript API where most of the functions are asynchronous. The API is the [WebKit JavaScript Database API][1] 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 [1]:
It'll be interesting to see if you do get an answer, because I suspect it's just not possible (at least without thread support, which Safari isn't going to give you).

以上就是Pattern for wrapping an Asynchronous JavaScript function to make it synchronous的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准