[英]How to get Entities synchronously while using Nodejs
You can write a helper function which returns Promise
to make it synchronous
(or simulate it) 您可以编写一个帮助程序函数,该函数返回
Promise
使其synchronous
(或模拟)
function getSome(mytable, hometasks, num)
return new Promise((resolve, reject) => {
tableSvc.retrieveEntity(mytable, hometasks, num, function(error, result, response){
if(!error){
resolve(entity // result or response etc)
} else {
reject(error)
}
});
})
Then you can use elsewhere in your code with async/await
(to pause execution) like 然后,您可以在代码中的其他地方使用
async/await
(以暂停执行),例如
Note you can use await
only inside async
function 注意您只能在
async
函数中使用await
async function useData() {
const data = await getSome('mytable', 'hometasks', '1');
// use data here
}
or with plain promise as (Note, this does not pause execution, code inside then
is a callback function again) 或带有简单的承诺(请注意,这不会暂停执行,里面的代码
then
是回调函数)
const data = getSome('mytable', 'hometasks', '1');
data.then(res => // do something)
Also looks like cosmos
have now sdk
with Promise
support. 看起来
cosmos
现在已经有了Promise
支持的sdk
。
Read more about Promise
and async/await
on MDN 阅读有关在MDN上的
Promise
和async/await
更多信息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.