Pattern for wrapping an Asynchronous JavaScript function to make it synchronous

前端 未结 7 1396
迷失自我
迷失自我 2020-12-09 04:33

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 functionali

7条回答
  •  时光说笑
    2020-12-09 04:47

    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 ;)

提交回复
热议问题