Javascript ES6/7等待异步函数

Javascript ES6/7等待异步函数,javascript,asynchronous,async-await,es6-promise,ecmascript-2017,Javascript,Asynchronous,Async Await,Es6 Promise,Ecmascript 2017,我读到关于等待停止执行,而承诺没有得到解决。我正在尝试在我的React native应用程序中执行以下操作: static async getFromStorage(key) { const value = await AsyncStorage.getItem(key); console.log(value); return value; } console.log(Class.getFromStorage("test")); 但是我得到了以下信息,而不是Chrome不允许我复制

我读到关于等待停止执行,而承诺没有得到解决。我正在尝试在我的React native应用程序中执行以下操作:

static async getFromStorage(key) {
  const value = await AsyncStorage.getItem(key);
  console.log(value);
  return value;
}

console.log(Class.getFromStorage("test"));
但是我得到了以下信息,而不是Chrome不允许我复制时间戳的实际值…:

Promise{u 45:0,_81:0,_65:null,_54:null}运行console.log的代码也应该标记为async,并且还应该等待getFromStorage的返回值

e、 g.这将有助于:

async function waitForStorage() {
   console.log(await getFromStorage());
}
waitForStorage(); // now the console messages should be in the correct order
或者你可以等待承诺的决议:

getFromStorage("token").then(someVar => checkToken(someVar))

我更新了这篇文章,因为这不是我真正想要的。@pumpinseed除非你用async标记范围调用wait,否则你想要的是不可能的。最简单的解决方案就是等待承诺,例如getFromStoragetoken.thensomeVar=>checkTokensomeVarYes我做到了谢谢。你能更新帖子吗?我们也能这样做吗?async waitForStorage:=>{}您是否使用诸如babel之类的Transbilier?es6/7并非到处都支持。async await的全部要点是以同步方式编写异步代码。等待语句之后的任何内容都不应执行,直到等待的承诺被解决或拒绝。此解决方案的问题是,我需要在类中立即全局地使用它,而异步/await不能解决它。没有什么能解决那个问题。使用回调或使所有内容异步。即使您使用了异步,它也不会立即在全球范围内可用。@Pumpkinsed我没有投您反对票。我很少投反对票,我试图帮助他们——如果我投了某人的反对票,我会发表评论让他们知道原因。在我看来,那些不加评论就投否决票的人都是些无赖。只是你的调用函数不是异步的。async不仅仅神奇地使异步代码同步运行;它只是将回调隐藏在承诺后面,然后您必须向承诺中添加回调。它不会也不能解决您的设计问题。FWIW,异步函数是ES2017的一部分,而不是ES6或ES7的一部分。
async function waitForStorage() {
   console.log(await getFromStorage());
}
waitForStorage(); // now the console messages should be in the correct order
getFromStorage("token").then(someVar => checkToken(someVar))