我需要在JavaScript中编写一个函数,它从调用异步函数返回一个状态.但是,调用者只接收该值,并且不提供回调函数.我尝试过类似的东西:
function getState() {
var ret = null;
asyncCall("request",
function() { ret = "foo"; } // callback
);
while (ret === null)
; // block on the asynchronous call
return ret;
}
Run Code Online (Sandbox Code Playgroud)
然而,循环永远不会结束......
有任何想法吗?谢谢.
同时通过IndexedDB的美妙的世界我的方式,我碰到像代码这个来自Mozilla的测试套件:
/**
* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/
*/
var testGenerator = testSteps();
function testSteps()
{
const IDBObjectStore = Components.interfaces.nsIIDBObjectStore;
const name = this.window ? window.location.pathname : "Splendid Test";
const description = "My Test Database";
var data = [
{ name: "inline key; key generator",
autoIncrement: true,
storedObject: {name: "Lincoln"},
keyName: "id",
keyValue: undefined,
},
{ name: "inline key; no key generator",
autoIncrement: false,
storedObject: {id: 1, name: "Lincoln"},
keyName: "id",
keyValue: undefined, …Run Code Online (Sandbox Code Playgroud)