如何在没有 setTimeout 的情况下暂停 Javascript 异步函数?



我有一个函数来检查数据库中是否存在令牌。问题是返回该bool值需要一些时间,我有点需要暂停该函数,以便该函数意识到该令牌已存在并再次运行查询。

const registerToken = dispatch => {
var tokenExisted = null
do {
let token = generateRandomToken();
firebase.database().ref(`/Token`).orderByChild("token").equalTo(token).once("value", snapshot => { // check whether token exists
if (!snapshot.val()) {  // if token not exist
token = false;
// register token to firebase
} else {
token = true; // continue the loop to generate a new token and query again
}
})
} while (tokenExisted === true);
}

我的设置基本上是一个do-while循环,当函数第一次被调用时tokenExisted = null,则将生成一个随机的 4 位令牌,并将向 Firebase 发送一个查询并验证其令牌是否存在。

如果令牌存在,则tokenExisted = true.我希望它执行赋值,但 Javascript 的单线程性质将在查询返回任何内容之前到达循环的末尾。

我想使用setTimeout并在tokenExisted = null时定期添加一些少量时间以保持安全保护,以便当查询函数返回任何内容时,函数将始终捕获。

有没有人有更好的方法来实现同样的事情?

您可能希望以递归方式调用函数本身。

const registerToken = dispatch => {
let token = generateRandomToken();
const tokenObjectRef = firebase.database().ref(`/Token`);
tokenObjectRef.orderByChild("token").equalTo(token).once("value")
.then(snapshot => {
if (!snapshot.val()) {
// success!
} else {
registerToken(dispatch) // call itself again
}
})
.catch(error => {} ))
}

逻辑是,如果过程失败并且需要新查询(如果需要(,令牌将在每次新迭代期间刷新。

注意:避免在async逻辑中使用do-while。提前仔细计划,因为您可能会遇到很多逻辑错误并且很难追踪。

递归调用函数。

function get_token_then(callback_when_token_found) {
firebase.database().etc.etc(function (data) {
if (data == what_you_want) {
callback_when_token_found(data);
} else {
// You might want to wrap this in setTimeout in order to throttle your database calls
get_token_then(callback_when_token_found);
}
}
}

最新更新