我很确定这是不可能的,但以防万一。
假设我有一堆添加到查询构建器的函数:
let query = User.query();
query = filterName(query, name);
query = filterLocation(query, location);
const users = await query;
这很好。但是,如果我需要这些函数之一是异步的(例如获取一些数据),我不能await
该函数,因为它会解析整个查询。
async function filterLocation(query, location) {
const data = await ...;
return query.where(...);
}
...
query = await filterLocation(query, location); // `query` is now resolved to a list of users, but I want it to remain a promise
有没有办法让JS不解析filterLocation
返回的promise?我必须将它包装在一个对象中吗?
答案 0 :(得分:-4)
await
只能在 async
函数内使用。
...
(async function(){
query = await filterLocation(query, location);
})();
...
filterLocation(query, location)
.then(function (query){
...
});