javascript - JS | 如何使用 async await 迭代地运行函数
问题描述
我有一个功能
async function go(text)
{
var response = await say(text);
document.write(response + '<br>')
}
和一个承诺
function say(text)
{
return new Promise((resolve, reject) => {
if (enterPressed) resolve(text);
});
}
每次用户按下回车时,我都想运行 go 函数。例如,脚本是:
go ("hello");
go ("goodbye")
我不希望这些功能立即运行,我要先打招呼,然后等待进入,然后再见。有什么方法吗?
解决方案
怎么样:
async function foo() {
while (true) {
await go("hello");
await go("goodbye");
}
}
foo();
推荐阅读
- android - 在android上关闭kivy webview
- javascript - 如何使用http客户端将多个值传递给角度9+的GET API中的参数
- arrays - 比较数组中的重复证书并删除过期日期最早的匹配项
- python - Django 翻译文件 - 将旧版翻译集成到 .po 文件中
- php - Vue.js 脚本在我的 heroku Laravel 应用程序中不起作用
- python - 字典中pandas DataFrame的错误列映射
- android - 如何在 kotlin 多平台或任何替代方案中使用 Bundle
- visual-studio-2019 - Visual Studio 2019 中的 FakesAssemblies 来自哪里?
- xml - 如何使用漂亮的汤检索嵌入在标签中的 xml 文件中的特征
- npm - NPM - 错误,运行任何命令时找不到模块