如果找不到结果,如何停止重新思考数据库调用?
How to stop rethink database call if cannot find result?
I 运行 RethinkDB 命令与 Node.js (babel) 异步调用:
let user = await r.table('users').filter({key: key}).limit(1).run();
如果数据库找不到结果,如何停止异步调用?
使用await
函数意味着节点将等待异步r.table(...
命令到return,然后再继续下一行代码,这意味着它的行为在逻辑上就好像它是同步代码。
当 RethinkDB 找到具有指定键的第一个 'user' 文档时,您的特定命令应该 return。如果找不到结果,则无需 "stop",它会在 (a) 找到结果或 (b) 完成扫描整个 table.
后立即停止
通常 "stopping" node/javascript 中的异步代码是不可能的,但您可以限制等待异步方法的时间。这是使用 Promise.race() 函数的示例。
/*
* toy async function
*
* returns a promise that resolves to the specified number `n`
* after the specified number of seconds `s` (default 2)
*/
const later = (n, s=2) => {
return new Promise(resolve => {
setTimeout(() => resolve(n), s*1000);
})
}
/*
* returns a promise that rejects with `TIMEOUT_ERROR` after the
* specified number of seconds `s`
*/
const timeout = (s) => {
return new Promise((resolve, reject) => {
setTimeout(() => reject("TIMEOUT_ERROR"), s*1000)
})
}
/*
* Example 1: later finished before timeout
* later resolves after 1 second, timeout function rejects after 3 seconds
* so we end up in the `.then` block with `val == 42`
*/
Promise.race([later(42, 1), timeout(3)])
.then(val => {
// do somethign with val...
console.log(val)
}).catch(err => {
if (err === "TIMEOUT_ERROR") {
console.log("we timed out!")
} else {
consle.log("something failed (but it was not a timeout)")
}
});
/*
* Example 2 (using async/await syntax): we timeout before later returns.
* later resolves after 3 seconds, timeout function rejects after 2 seconds
* so we end up in the `.catch` block with `err == "TIMEOUT_ERROR"`
*/
try {
const val = await Promise.race([later(11, 3), timeout(2)]);
// do something with val...
} catch (err) {
if (err === "TIMEOUT_ERROR") {
console.error("we timed out!")
} else {
console.error("something failed (but it was not a timeout)")
}
}
I 运行 RethinkDB 命令与 Node.js (babel) 异步调用:
let user = await r.table('users').filter({key: key}).limit(1).run();
如果数据库找不到结果,如何停止异步调用?
使用await
函数意味着节点将等待异步r.table(...
命令到return,然后再继续下一行代码,这意味着它的行为在逻辑上就好像它是同步代码。
当 RethinkDB 找到具有指定键的第一个 'user' 文档时,您的特定命令应该 return。如果找不到结果,则无需 "stop",它会在 (a) 找到结果或 (b) 完成扫描整个 table.
后立即停止通常 "stopping" node/javascript 中的异步代码是不可能的,但您可以限制等待异步方法的时间。这是使用 Promise.race() 函数的示例。
/*
* toy async function
*
* returns a promise that resolves to the specified number `n`
* after the specified number of seconds `s` (default 2)
*/
const later = (n, s=2) => {
return new Promise(resolve => {
setTimeout(() => resolve(n), s*1000);
})
}
/*
* returns a promise that rejects with `TIMEOUT_ERROR` after the
* specified number of seconds `s`
*/
const timeout = (s) => {
return new Promise((resolve, reject) => {
setTimeout(() => reject("TIMEOUT_ERROR"), s*1000)
})
}
/*
* Example 1: later finished before timeout
* later resolves after 1 second, timeout function rejects after 3 seconds
* so we end up in the `.then` block with `val == 42`
*/
Promise.race([later(42, 1), timeout(3)])
.then(val => {
// do somethign with val...
console.log(val)
}).catch(err => {
if (err === "TIMEOUT_ERROR") {
console.log("we timed out!")
} else {
consle.log("something failed (but it was not a timeout)")
}
});
/*
* Example 2 (using async/await syntax): we timeout before later returns.
* later resolves after 3 seconds, timeout function rejects after 2 seconds
* so we end up in the `.catch` block with `err == "TIMEOUT_ERROR"`
*/
try {
const val = await Promise.race([later(11, 3), timeout(2)]);
// do something with val...
} catch (err) {
if (err === "TIMEOUT_ERROR") {
console.error("we timed out!")
} else {
console.error("something failed (but it was not a timeout)")
}
}